如何迭代字典並在迭代循環期間檢查輸入值的有效性?這個問題在代碼中描述:如何迭代字典並檢查輸入的有效性?
mydict ={'Kansas': 0, 'Dallas': 0, 'Alabama': 0}
for key, value in mydict.items():
inp = int(input('Your number for: ' + key))
if inp > 0:
value = inp
else:
print('enter again') #<---how can I jump back to the key
# for which the entry was false
# and ask again for the entry?
我想補充一個try /除用於'ValueError'用戶輸入一個非數字。 –