我试着做一个临时的加密器,但似乎没有什么问题,我的输出是几个字符短。的帮助!
这是我的代码:
#Encrypter v1
import random, os, sys
inputstring = input("What is your sentence?(Remove all punctuation!)n")
inputstringnum = input("How many levels of encryptiion? Maximum encrytion lentgh is 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 the index
caps_list = list("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ")
output_list = []
for 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 and 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)
如前所述,迭代var1
并从中弹出项会导致您的第一个问题,即输出中的字符少于输入中的字符。
你也有过于复杂的代码,包括:
- 使用
list('abc')
代替直接使用字符串进行迭代超过或搜索索引。 - 与
True
的比较在python中不需要:if a:
或if t in ['a','b']:
在python中是完全有效的测试。 - 使用手工制作的字符列表。可以使用
ord
内置函数或string
模块中的ascii_lowercase
和ascii_uppercase
字符串。
我会将你的代码简化为:
def list_changer(input_string, encrypt_level):
output_list = []
for character in input_string:
char = ord(character) # convert character to ascii code
if not ((96 < char < 123) or (64 < char < 91)):
# if not ascii alphabetic value, ignore it
output_list.append(character)
continue
offset = 97 if char > 96 else 65 # check whether it is upper or lower case
char = (char - offset + encrypt_level) % 26 # encode it
character = chr(char + offset) # convert back to a character
output_list.append(character)
return ''.join(output_list)
if __name__ == "__main__":
inputstring = input("What is your sentence?n")
encrypt = int(input("How many levels of encryption?n"))
computed_string = list_changer(inputstring[::-1], encrypt)
print(computed_string)
首先,==
和in
由于操作符的改变而表现不佳。
a in ['a','b'] == True
当它变成- (a in ['a','b']) and (['a','b'] == True)
时,上面的返回False。
您不需要检查True
,只需在条件中直接使用其结果。
其次,当你这样做时-
for item in var1:
您正在迭代var1
的元素,那么如果您执行var1.pop(0)
,它将错过下一个元素。
第三,你的第二个if
应该是elif
,否则它会导致相同的元素被加密并放入列表中,同时也会导致未加密的元素被添加到列表中。
代码
import random, os, sys
inputstring = input("What is your sentence?(Remove all punctuation!)n")
inputstringnum = input("How many levels of encryptiion? Maximum encrytion lentgh is 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 the
index
caps_list = list("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ")
output_list = []
for item in var1:
tmp2 = item
if tmp2 in alphabet_list:
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)
elif tmp2 in caps_list:
tmp3 = caps_list.index(tmp2)
tmp3 = int(tmp3)
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)
问题在于list_changer
函数内的这2行-
for item in var1:
tmp2 = var1.pop(0)
你从你迭代的列表中跳出来。
tmp2 = item
代替
tmp2 = var1.pop(0)
这就是为什么你得到错误数量的结果。
基本上,你的list_changed
函数应该是-
def list_changer(var1, crypt_num):
alphabet_list = list("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz") #To find the index
caps_list = list("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ")
output_list = []
for item in var1:
tmp2 = item
if tmp2 in alphabet_list:
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)
elif tmp2 in caps_list:
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
-问题1
tmp2 in alphabet_list == True
将始终生成false
,因为您的alphabet_list
是一个列表,其计算结果为-
tmp2 in (alphabet_list == True)
用-
if tmp2 in alphabet_list:
-问题2
if tmp2 in caps_list == True and tmp2 in caps_list != True:
永远不会成立。
替换为-
elif tmp2 in caps_list