Issues with while loop, while (var != 0): does not work, but while (var !=''): works - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Issues with while loop, while (var != 0): does not work, but while (var !=''): works (/thread-17524.html) |
Issues with while loop, while (var != 0): does not work, but while (var !=''): works - danilo - Apr-14-2019 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? 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() RE: Issues with while loop, while (var != 0): does not work, but while (var !=''): works - Larz60+ - Apr-14-2019 typo: it's capitals I see you have both, moved too quickly looks like capital is text, so can't test for 0 try: while capital != '0': # Here is the problem RE: Issues with while loop, while (var != 0): does not work, but while (var !=''): works - danilo - Apr-15-2019 Awesome!! now it's working as expected, thanks a lot!! |