i tried making makeshift encrypter, nothing seems wrong , output several characters short. help!
this code:
#encrypter v1 import random, os, sys inputstring = input("what sentence?(remove punctuation!)\n") inputstringnum = input("how many levels of encryptiion? maximum encrytion lentgh 24.\n") inputstringnum1 = int(inputstringnum) #code def list_randomizer(inputstring1): inputlist = list(inputstring1) outputlist = inputlist[::-1] return outputlist def list_changer(var1, crypt_num): alphabet_list = list("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz") #to find index caps_list = list("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz") output_list = [] item in var1: tmp2 = var1.pop(0) if tmp2 in alphabet_list == true: tmp3 = alphabet_list.index(tmp2) tpm3 = int(tmp3) #failsafe object_int = tmp3 + crypt_num #encrpyting on desired depth tmp4 = alphabet_list[object_int] output_list.append(tmp4) if tmp2 in caps_list == true , tmp2 in caps_list != true: tmp3 = caps_list.index(tpm2) tmp3 = int(imp3) object_int = tmp3 + crypt_num tmp4 = caps_list[object_int] output_list.append(tmp4) else: output_list.append(tmp2) return output_list temp1 = list_changer(list_randomizer(inputstring), inputstringnum1) print(temp1)
as other stated, iterating on var1 and popping items leads first issue of having less character in output in input.
you have overly complicated code including:
- usage of
list('abc')instead of directly using strings iterate on or search indexes. - comparisons against
truenot necessary in python:if a:orif t in ['a','b']:valid tests in python. - usage of handcrafted lists of characters. either use
ordbuiltin function orascii_lowercase,ascii_uppercasestringsstringmodule.
i’d simplify code to:
def list_changer(input_string, encrypt_level): output_list = [] character in input_string: char = ord(character) # convert character ascii code if not ((96 < char < 123) or (64 < char < 91)): # if not ascii alphabetic value, ignore output_list.append(character) continue offset = 97 if char > 96 else 65 # check whether upper or lower case char = (char - offset + encrypt_level) % 26 # encode character = chr(char + offset) # convert character output_list.append(character) return ''.join(output_list) if __name__ == "__main__": inputstring = input("what sentence?\n") encrypt = int(input("how many levels of encryption?\n")) computed_string = list_changer(inputstring[::-1], encrypt) print(computed_string)
Comments
Post a Comment