Python Forum
Issues with while loop, while (var != 0): does not work, but while (var !=''): works
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with while loop, while (var != 0): does not work, but while (var !=''): works
#1
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()
Reply
#2
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
Reply
#3
Awesome!! now it's working as expected, thanks a lot!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop does not work in python Ola92 11 4,830 Jan-13-2020, 06:27 PM
Last Post: Ola92
  Issues with Inserting Values into an Empty List with a While Loop TommyMer 2 3,778 Sep-12-2018, 12:43 AM
Last Post: TommyMer
  Hi, my loop yes/no part doesn't work and I really need help fordxman5 2 2,610 Feb-14-2018, 11:38 PM
Last Post: Larz60+
  loop issues kanwal121 4 3,401 Dec-18-2017, 01:06 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020