Reputation: 63
countries={'TW':'Taiwan','JP':'Japan','AUS':'Australia'}
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer in ('yes'):
return True
if answer in ('no'):
return False
print countries
add_country()
I just started learning Python. Above code isn't correct, can somebody fix it for me? Basically I just want to repeat the loop once if answer is yes, or break out of the loop if answer is no. The return True/False doesn't go back to the while loop?
Upvotes: 0
Views: 23295
Reputation: 879113
break
to break out of the while loop:answer
is in ('yes',)
, since the
while True
loop will continue looping by default:answer in ('no')
is the same as answer in 'no'
, which would only
be True if answer
is 'n'
or 'o'
or 'no'
. That's probably not
what you mean. Better to use answer == 'no'
. If you had added a comma, as in answer in ('no',)
, then ('no',)
would be a tuple and the condition would be met if answer
is equal to one of the items in the tuple. The comma has a lot of significance here!def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer == 'no':
break
print countries
Upvotes: 0
Reputation: 22728
You need to use the break
command to exit the loop.
countries={'TW':'Taiwan','JP':'Japan','AUS':'Australia'}
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer in ('yes'):
answer = True
break
if answer in ('no'):
answer = False
break
print countries
return answer
add_country()
Upvotes: 1
Reputation: 47968
You don't want to return
until the answer is 'no'. Return will conclude the execution of the function. You want to continue
the loop.
Upvotes: 0
Reputation: 500167
return
terminates the function. To stop the loop, use break
.
countries={'TW':'Taiwan','JP':'Japan','AUS':'Australia'}
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer != 'yes':
break
print countries
add_country()
Notice how I've also changed answer in ('no')
since that didn't do what you expected (it checked whether answer
was either 'n'
or 'o'
).
Upvotes: 0
Reputation: 181270
You need to break out of the loop instead of returning the function.
It will be something like:
if answer == 'yes':
break
if answer == 'no':
pass
# do nothing
You don't need the if answer == 'no'
part. The return
statement will take your execution out of the function.
Upvotes: 0
Reputation: 226181
It just needs minor fixups:
def add_country():
while True:
new_short=raw_input('Country Name in short:')
new_full=raw_input('Country Name in full:')
countries[new_short]=new_full
answer=raw_input('want to add more?')
if answer == 'no':
return False
print countries
add_country()
Upvotes: 3