Skip to content Skip to sidebar Skip to footer

Breaking While Loop With Function?

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. H

Solution 1:

You can work with an exception:

classAdventureDone(Exception): passdefyn(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:
    whileTrue:
        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.

Solution 2:

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.

defyn(input, yes, no):
    input = input.lower()
    ifinput == 'y'orinput == 'yes':
        print (yes)
        return1elifinput == 'n'orinput == 'no':
        print (no)
        return2else:
        return0

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

whileTrue:
    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.

Solution 3:

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:

defyn(x, f, g):
    if (x) == 'y':
        print (f)
        returnFalseelif (x) == 'n':
        print (g)
        returnTrue

name = raw_input('What is your name, adventurer? ')
print'Nice to meet you, '+name+'. Are you ready for your adventure?'whileTrue:
    ready = raw_input('y/n: ')
    if (yn(ready, 'Good, let\'s start our adventure!', 'That is a real shame.. Maybe next time')):
        break

Solution 4:

One approach would be to have yn return a boolean value which then would be used to break out of the loop. Otherwise a break within a function cannot break out of a loop in the calling function.

defyn(x, f, g):
    if (x) == 'y':
        print (f)
        returnTrueelif (x) == 'n'print (g)
        returnFalse

name = raw_input('What is your name, adventurer? ')
print'Nice to meet you, '+name+'. Are you ready for your adventure?'
done = Falsewhilenot done:
    ready = raw_input('y/n ')
    done = yn(ready, 'Good, let\'s start our adventure!', 'That is a real shame.. Maybe next time')

Solution 5:

Using break , you can come out of the loop even if the condition for the end of the loop is not fulfilled. You cant have break because 'if /elif ' is not a loop, its just a conditional statement.

Post a Comment for "Breaking While Loop With Function?"