Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

breaking while loop with function? [duplicate]

I am trying to make a function that has an if/elif statement in it, and I want the if to break a while loop.. The function is for a text adventure game, and is a yes/no question. Here is what i have come up with so far..

def yn(x, f, g):
    if (x) == 'y':
         print (f)
         break
    elif (x) == 'n'
         print (g)

name = raw_input('What is your name, adventurer? ')
print 'Nice to meet you, '+name+'. Are you ready for your adventure?'

while True:
    ready = raw_input('y/n ')
    yn(ready, 'Good, let\'s start our adventure!', 
       'That is a real shame.. Maybe next time')

Now I'm not sure if I am using the function right, but when I try it out, it says I can't have break in the function. So if someone could help me with that problem, and if you could help me if the function and calling the function itself is formatted wrong, that would be very much appreciated.

like image 477
Ryan Hosford Avatar asked Apr 18 '13 02:04

Ryan Hosford


3 Answers

You can work with an exception:

class AdventureDone(Exception): pass

def yn(x, f, g):
    if x == 'y':
         print(f)
    elif x == 'n':
         print(g)
         raise AdventureDone

name = raw_input('What is your name, adventurer? ')
print 'Nice to meet you, '+name+'. Are you ready for your adventure?'

try:
    while True:
        ready = raw_input('y/n ')
        yn(ready, "Good, let's start our adventure!",
           'That is a real shame.. Maybe next time')
except AdventureDone:
    pass
    # or print "Goodbye." if you want

This loops the while loop over and over, but inside the yn() function an exception is raised which breaks the loop. In order to not print a traceback, the exception must be caught and processed.

Edit (after more than nine years):

It seems to me that I forgot the situation for "y". So, let's proceed in a different way:

def yn(x, f, g):
    if x == 'y':
         print(f)
         return True
    elif x == 'n':
         print(g)
         return False
    return None # undefined case

name = raw_input('What is your name, adventurer? ')
print 'Nice to meet you, '+name+'. Are you ready for your adventure?'

decided = None
while decided is None:
    ready = raw_input('y/n ')
    decided = yn(ready, "Good, let's start our adventure!",
           'That is a real shame.. Maybe next time')

# decided is not None now, so can only be False or True.
if decided:
    play()
else:
    pass
    # or print "Goodbye." if you want
like image 68
glglgl Avatar answered Oct 19 '22 10:10

glglgl


You will need to change the break inside your function to a return, and you need to have an else statement in case that the user did not provide you with the correct input. Finally you need to turn the call in your while loop into a if statement.

This will allow you to break the while statement if the player enters the desired command, if not it will ask again. I also updated your yn function to allow the user to use both lower and upper case characters, as well as yes and no.

def yn(input, yes, no):
    input = input.lower()
    if input == 'y' or input == 'yes':
        print (yes)
        return 1
    elif input == 'n' or input == 'no':
        print (no)
        return 2
    else:
        return 0

name = raw_input('What is your name, adventurer? ')
print 'Nice to meet you, %s. Are you ready for your adventure?' % name

while True:
    ready = raw_input('y/n ')
    if yn(ready, 'Good, let\'s start our adventure!',
       'That is a real shame.. Maybe next time') > 0:
        break

The idea behind this is pretty simple. The yn function has three states. Either the user responded with yes, no or invalid. If the user response is either yes or no, the function will return 1 for yes, and 2 for no. And if the user does not provide valid input, e.g. a blank-space , it will return 0.

Inside the while True: loop we wrap the yn('....', '....') function with an if statement that checks if the yn function returns a number larger than 0. Because yn will return 0 if the user provides us with an valid input, and 1 or 2 for valid input.

Once we have a valid response from yn we call break, that stops the while loop and we are done.

like image 38
eandersson Avatar answered Oct 19 '22 10:10

eandersson


You need to break out of the while loop within the loop itself, not from within another function.

Something like the following could be closer to what you want:

def yn(x, f, g):
    if (x) == 'y':
        print (f)
        return False
    elif (x) == 'n':
        print (g)
        return True

name = raw_input('What is your name, adventurer? ')
print 'Nice to meet you, '+name+'. Are you ready for your adventure?'

while True:
    ready = raw_input('y/n: ')
    if (yn(ready, 'Good, let\'s start our adventure!', 'That is a real shame.. Maybe next time')):
        break
like image 4
Duane Moore Avatar answered Oct 19 '22 10:10

Duane Moore