为什么我的输出列表缺少几项?

  • 本文关键字:几项 列表 输出 python
  • 更新时间 :
  • 英文 :


我试着做一个临时的加密器,但似乎没有什么问题,我的输出是几个字符短。的帮助!

这是我的代码:

#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_lowercaseascii_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

最新更新