試圖通過字典來做到這一點,但它不起作用。有針對這個的解決方法嗎? 該任務是用戶被要求猜測從字典中隨機選擇的國家的首都。當用戶寫回答時,它會變成價值並檢查是否與國家/地區關鍵字匹配。字典中的隨機密鑰在作爲輸入輸入時無法識別它的值
import random
capitals = {'England': 'London', 'Spain': 'Madrid', 'France': 'Paris'}
rand= (random.choice (list(capitals)))
for i in capitals:
inp= input("what's is the capital of "+rand+ ": ")
if inp.upper()==capitals[i].upper():
print ("correct")
break
else:
print ("think again")
你問rand'的'資本,但檢查i'的'資本。 – Ryan
'else'應該與現在正在工作的'for' – alfasin