Python Forum

Full Version: If elif else statement not functioning
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am coding a text based game and I need to find out the name of the players character, and confirm that it is spelled correctly.

def name_ask():
    print('Hello, what is your name, hero?\n'),
    player_name = input('> ')
    print('Hello ' + (player_name) + '. Is your name correct?')
    asknamecorrect = input('> ')
    if asknamecorrect == ('yes', 'Yes'):
        print('Good.')
    elif asknamecorrect == ('no', 'No'):
        name_ask()
    else:
        print('Please answer with a yes or a no.')
        name_ask()
The interaction should be:
Output:
Hello, what is your name hero? > name Hello name. Is your name correct? >yes Good.
What I get:
Output:
Hello, what is your name hero? > name Hello name. Is your name correct? > yes Please answer with a yes or a no. Hello, what is your name, hero? >
It turns into an endless loop. How do I fix this? Thank you.
you need to check for membership, using in, not for equality ==. Now you check if user input is equal to specific tuple, not that user input is in that tuple.
Also calling the function recursively like this is bad pattern, use loop instead
What do you mean by "Now you check if user input is equal to specific tuple". I do not have any tuple functions. Should I here?

How do you call functions in a loop in this situation? The functions don't repeat themselves.
this: ('yes', 'Yes') is a tuple. Also this ('no', 'No') is a tuple.

>>> user_input = 'Yes'
>>> user_input in ('yes', 'Yes')
True
>>> user_input == ('yes', 'Yes')
False
I don't mean to call the function in a loop. Use a loop to ask the user until you get yes or Yes
Thank you so much! I understand now.