拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 为什么这个程序不应该将“无”字符串添加到所需的键中?

为什么这个程序不应该将“无”字符串添加到所需的键中?

白鹭 - 2022-02-22 1940 0 0

在我的程序中,我有一个名为的字典,the_dictionary_list描述如下:

the_dictionary_list = {'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 'Cuerpo': ['Cuerpo_cangrejo.png'], 'Fondo': ['Oceano.png'], 'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'], 'Pinzas': ['Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 'Puas': ['Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}

现在,用户将被要求决定他是否想在字典中的阵列中添加一个“”项目,然后他将被问到将用None文本更新哪个可用的字典键,然后用户输入键的名称作为输入,输入可以在单引号内,也可以不在单引号内,只要它是一个有效的键,如果输入在字典中不存在,他将不得不再次输入,直到他没有'不想再更新可用的字典键之一:

entrada = input('All right, do you want to add a "None" item to an array in the dictionary? (y/n):')
while True:

if entrada == 'y':
    while True:
        key_entrada = input('Cool, now tell me at which key do you want me to add a "None" item? Type only a valid key name:')
        if key_entrada.startswith("'") & key_entrada.endswith("'"):
            if key_entrada in the_dictionary_list:
                the_dictionary_list[key_entrada].insert(0, 'None')
                entrada = input('Do you want to add another one? (y/n):')
                if entrada == 'n':
                    print('ta weno')
                    break
                if entrada == 'y':
                    break
                else:
                    break
            else:
                key_entrada = input('That input does not exist in the dictionary, try again, Type only a valid key name:')
                
        else:
            print('you did not put single quotation marks, let me add them to your input')
            comillas = str("'") key_entrada str("'")
            if comillas in the_dictionary_list:
                the_dictionary_list[comillas].insert(0, 'None')
                entrada = input('Do you want to add another one? (y/n):')
                if entrada == 'n':
                    print('ta weno')
                    break
                if entrada == 'y':
                    break
                else:
                    break                        
            else:
                key_entrada = input('That input does not exist in the dictionary, try again, Type only a valid key name:')
            
if entrada == 'n':
    print('weno')
    break

else:
    entrada = input("Invalid Input, Type 'y' or 'n' without single quotation marks: ")

它应该按预期作业,但是经过测验后,我得到了以下输出

我很确定我的编码是正确的,但也许我在这里省略了一些重要步骤或做了一些“非法”的事情,所以我想听听你们认为是什么让这个程序无法按预期作业?

所需的最终输出应该是这样的:

the_dictionary_list:
{'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 'Cuerpo': ['Cuerpo_cangrejo.png'], 'Fondo': ['Oceano.png'], 'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'], 'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}

对于用户决定只更新PinzasPuas键的情况。

uj5u.com热心网友回复:

问题解决了,感谢@JohnGordon,他以某种方式照亮了我:

entrada = input('All right, do you want to add a "None" item to an array in the dictionary? (y/n):')

while True:

if entrada == 'y':
    key_entrada = input('Cool, now tell me at which key do you want me to add a "None" item? Type only a valid key name:')
    while True:            
        if key_entrada in the_dictionary_list:
            the_dictionary_list[key_entrada].insert(0, 'None')
            key_entrada = input('Do you want to add another one? (y/n):')
            if key_entrada == 'n':
                entrada = 'n'
                break
            if key_entrada == 'y':
                key_entrada = input('Cool, now tell me at which key do you want me to add a "None" item? Type only a valid key name:')
            else:
                key_entrada = input("Invalid Input, Type 'y' or 'n' without single quotation marks: ")
        else:
            key_entrada = input('That input does not exist in the dictionary, try again, Type only a valid key name:')               
                               
if entrada == 'n':
    print('weno')
    break  

elif entrada != 'y' or entrada != 'n':
    entrada = input("Invalid Input, Type 'y' or 'n' without single quotation marks: ")**strong text**
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *