Python Forum
If elif else statement not functioning
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If elif else statement not functioning
#1
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.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thank you so much! I understand now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question If, elif, and else statement not working PickleScripts 3 899 Mar-30-2023, 02:53 PM
Last Post: PickleScripts
  time.clock not functioning Oldman45 3 2,706 Apr-07-2021, 08:51 AM
Last Post: Oldman45
  I can`t find an IDE functioning in my laptop All_ex_Under 5 2,979 Aug-17-2020, 05:44 AM
Last Post: All_ex_Under
  Proper use of if..elif..else statement nick1941 2 2,421 Mar-06-2020, 11:22 PM
Last Post: nick1941
  Syntax Error (elif statement) Kanashi 0 3,668 Nov-20-2019, 11:29 PM
Last Post: Kanashi
  Whats the right way to refactor this Big if/elif/elif ? pitosalas 1 2,245 Jul-28-2019, 05:52 PM
Last Post: ichabod801
  Problem with elif statement Haddal99 2 2,278 May-20-2019, 09:26 AM
Last Post: avorane
  if-elif-else statement not executing laila1a 4 3,035 Jan-05-2019, 02:22 PM
Last Post: buran
  Data manipulation code running but not functioning correctly homotextual 1 2,167 Dec-30-2018, 03:19 PM
Last Post: ichabod801
  Unexpected input within an if/elif statement fier259 2 2,820 May-12-2018, 07:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020