在我的程式中,我有一個名為的字典,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: ")
它應該按預期作業,但是經過測驗后,我得到了以下輸出:
好的,您想在字典中的陣列中添加一個“無”項嗎?(y/n):y
酷,現在告訴我您希望我在哪個鍵添加“無”專案?只輸入一個有效的鍵名: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鍵的情況。
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**
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/395692.html
