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.
source share