Qin
Qin

Reputation: 63

Repeating while loop, until cancel

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

Answers (6)

unutbu
unutbu

Reputation: 879113

  • Use break to break out of the while loop:
  • There is no need to test if 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

tkone
tkone

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

g.d.d.c
g.d.d.c

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

NPE
NPE

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

Pablo Santa Cruz
Pablo Santa Cruz

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

Raymond Hettinger
Raymond Hettinger

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

Related Questions