Al_Iskander Al_Iskander - 6 days ago 5
Python Question

How to iterate through a dictionary and check validity of inputs?

how can I iterate over a dictionary and check input values for validity during the iterating loop? The problem is described in the code:

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?

Answer
for key, value in mydict.items():
    while True:
        try:
            inp = int(input('Your number for: ' + key))
        except:
            inp = 0

        if inp > 0:
                mydict[keu] = inp
                break
        print('enter again')
Comments