Apr-14-2019, 09:59 PM
Hi there,
I have an issue (line 44 of this code) when using this statement, it creates an infinite loop:
while capital != 0:
However, everything works fine when using this one:
while capital != '':
The idea for this program is to stop when 0 is the input not when you just press enter.
Here the whole code, any idea why this is happening?
I have an issue (line 44 of this code) when using this statement, it creates an infinite loop:
while capital != 0:
However, everything works fine when using this one:
while capital != '':
The idea for this program is to stop when 0 is the input not when you just press enter.
Here the whole code, any idea why this is happening?
def main(): capitals = create_capitals() current_state, value = capitals.popitem() capital = input(('What is the capital of ' + current_state + '? (or enter 0 to quit): ')) look_up(capitals, value, capital) def create_capitals(): capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', 'Arizona': 'Phoenix', 'Arkansas': 'Little Rock', 'California': 'Sacramento', 'Colorado': 'Denver', 'Connecticut': 'Hartford', 'Delaware': 'Dover', 'Florida': 'Tallahassee', 'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', 'Idaho': 'Boise', 'Illinois': 'Springfield', 'Indiana': 'Indianapolis', 'Iowa': 'Des Moines', 'Kansas': 'Topeka', 'Kentucky': 'Frankfort', 'Louisiana': 'Baton Rouge', 'Maine': 'Augusta', 'Maryland': 'Annapolis', 'Massachusetts': 'Boston', 'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', 'Mississippi': 'Jackson', 'Missouri': 'Jefferson City', 'Montana': 'Helena', 'Nebraska': 'Lincoln', 'Nevada': 'Carson City', 'New Hampshire': 'Concord', 'New Jersey': 'Trenton', 'New Mexico': 'Santa Fe', 'New York': 'Albany', 'North Carolina': 'Raleigh', 'North Dakota': 'Bismarck', 'Ohio': 'Columbus', 'Oklahoma': 'Oklahoma City', 'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', 'Rhode Island': 'Providence', 'South Carolina': 'Columbia', 'South Dakota': 'Pierre', 'Tennessee': 'Nashville', 'Texas': 'Austin', 'Utah': 'Salt Lake City', 'Vermont': 'Montpelier', 'Virginia': 'Richmond', 'Washington': 'Olympia', 'West Virginia': 'Charleston', 'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'} return capitals def look_up(capitals, value, capital): correct = 0 wrong = 0 while capital != 0: # Here is the problem if value == capital: print('That is correct.') correct = correct + 1 elif value != capital: print('That is incorrect.') wrong = wrong + 1 else: break current_state, value = capitals.popitem() capital = input(('What is the capital of ' + current_state + '? (or enter 0 to quit): ')) print('You had', correct, 'correct responses and', wrong, 'incorrect responses.') main()