Reputation: 21
I'm sorry if this is a ridiculous question but I'm just learning python and I can't figure this out. :)
My program is supposed to print the capital of any state that the user inputs. Sometimes it will work ten times in a row, other times it will work three times in a row, and then it will just stop after you type in a state. If I restart it and type in the state that it stopped on it will work just fine....for a random number of times and then it will stop again. What am I doing wrong? Also is my code awful? I had no idea what sort of code to use for this so I just kind of tossed in whatever I could make work.
x = str(raw_input('Please enter a sate: ' ))
while x == 'Alabama':
print 'Montgomery is the capital of', x
x = str(raw_input('Please enter a state: '))
while x == 'Alaska':
print 'Juneau is the capital of', x
x = str(raw_input('Please enter a state: '))
while x == 'Arizona':
print 'Phoenix is the capital of', x
x = str(raw_input('Please enter a state: ' ))
while x == 'Arkansas':
print 'Little Rock is the capital of', x
x = str(raw_input('Please enter a state: '))'
Upvotes: 1
Views: 265
Reputation: 78600
You mean to use multiple if
statements within one big while
loop, rather than multiple while
loops. In this code, once you're past one while loop, you never come back to it. This code will work only as long as you are giving it state names in alphabetical order.
Don't do it this way! There is a much better way to do it using python dictionaries.
capitals = {"Alabama": "Montgomery", "Alaska": "Juneau", "Arizona": "Phoenix", "Arkansas": "Little Rock"}
while True:
x = str(raw_input('Please enter a state: ' ))
if x in capitals:
print capitals[x], "is the capital of", x
Otherwise, you'll end up having 50 pairs of almost identical lines if you want to cover all 50 states.
Upvotes: 5
Reputation: 318508
In all honesty it is way worse then awful. However, you are most likely a beginner and thus such things happen.
For this task you should use a dict
that contains the country=>capital mappings and read the country name once:
capitals = {'Alabama': 'Montgomery',
'Alaska': 'Juneau',
...}
state = raw_input('Please enter a state: ')
if(state in capitals):
print '{0} is the capital of {1}'.format(capitals[state], state)
else:
print 'Sorry, but I do not recognize {0}'.format(state)
If you want to use a while
loop so the user can enter multiple states, you can wrap the whole code in a while True:
block and use if not state: break
right after the raw_input
line to break the loop if the user does not enter anything.
Upvotes: 0
Reputation: 309929
I don't think you understand the while
loop. Basically,
while condition:
dostuff()
does stuff while the condition is true. As soon as the condition is false, you move on. I think what you're looking for is something like:
x=True
while x
x=raw_input('please enter a state'):
if x == 'Alabama':
...
elif x == 'Alaska':
...
This will loop forever until the user just hits enter (bool('')
is False
in python)
However, a much better way to do it would be to use a dictionary:
state_capitals={'Alabama':'Montgomery', 'Alaska':'Juneau'}
x=True
while x
x=raw_input('please enter a state'):
print '{0} is the capital of {1}'.format(state_capitals[x],x)
with this way, it will raise a KeyError
when a bad capital is given (which you could catch using a try
block if you wanted to).
Upvotes: 1