Interrupting a cycle with a function?

I am trying to create a function in which there is an if / elif statement, and I want to interrupt the while loop if I would. 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\ start our adventure!', 'That is a real shame.. Maybe next time') 

Now I'm not sure if I use this function correctly, but when I try it, it says that I can not break the function. Therefore, if someone can help me with this problem, and if you could help me if the function and the call to the function itself are formatted incorrectly, that would be very appreciated.

+4
source share
6 answers

You can work with the exception of:

 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 start our adventure!", 'That is a real shame.. Maybe next time') except AdventureDone: pass # or print "Goodbye." if you want 

This is a while loop while over and over, but an exception is thrown inside the yn() function that interrupts the loop. In order not to print the trace, the exception must be caught and processed.

+5
source

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

Something like the following might 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\ start our adventure!', 'That is a real shame.. Maybe next time')): break 
+1
source

You will need to change the gap inside your function before returning, and you need to have an else in case the user has not provided you with the correct input. Finally, you need to include the call in the while loop in the if statement.

This will allow you to break the while statement if the player enters the desired team, if not requested again. I also updated your yn function to allow the user to use both lower and upper case characters, and 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\ 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. Any user answered yes, no or is invalid. If the user's 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, for example. empty space, it will return 0.

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

Once we have the correct answer from yn , we call break, which stops the while loop , and we are done.

+1
source

One approach would be to yn return a boolean value that will then be used to exit the loop. Otherwise, a break inside the function cannot exit the loop in the calling function.

 def yn(x, f, g): if (x) == 'y': print (f) return True elif (x) == 'n' print (g) return False name = raw_input('What is your name, adventurer? ') print 'Nice to meet you, '+name+'. Are you ready for your adventure?' done = False while not done: ready = raw_input('y/n ') done = yn(ready, 'Good, let\ start our adventure!', 'That is a real shame.. Maybe next time') 
0
source

Using break, you can exit the loop even if the loop termination condition is not met. You cannot break because "if / elif" is not a loop, but just a conditional statement.

0
source
 a = True def b(): if input("") == "Quit": global a a == False else: pass while a == True: print('Solution') 
0
source

All Articles