代码之家  ›  专栏  ›  技术社区  ›  NoahVerner

为什么这个程序在应该的时候不向所需的键添加“None”字符串?

  •  0
  • NoahVerner  · 技术社区  · 4 年前

    在我的程序中,我有一本字典叫做 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: ")
    

    它应该像预期的那样工作,但是在测试之后,我得到了以下输出 :

    好的,是否要将“无”项添加到 词典(是/否):是

    很好,现在告诉我你想让我在哪个键添加“无”项? 仅键入有效的密钥名:Pinzas

    您没有使用单引号,让我将它们添加到您的输入中

    该输入在字典中不存在,请重试,只键入一个 有效密钥名:'Pinzas'

    很好,现在告诉我你想让我在哪个键添加“无”项? 仅键入有效的密钥名:“Pinzas”

    该输入在字典中不存在,请重试,只键入一个 有效密钥名:

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

    期望的最终输出应该是这样的:

    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']}
    

    对于用户决定只更新 Pinzas Puas 钥匙。

    1 回复  |  直到 4 年前
        1
  •  0
  •   NoahVerner    4 年前

    问题解决了,多亏了@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**