Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conditional
#1
I am trying to design a condition yes/no statement questions and I cannot figure out what I am doing wrong :(
name = input("What is your name? ")
print ("Hello " + name + ", nice to meet you. ")
answer = input("Do you want to identify a mineral? (y/n)")
if answer == "y" or "Y":
  print ("Great," + name + " ,let's get started")
elif answer == "n" or "N":
  print ("Ok," + name + " ,come back when you have a mineral to identify")
buran write Jan-13-2021, 06:57 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
This is a precedence issue. Your if statement is parsed as:

if (answer == "y") or ("Y"):
Answer == "y" may not be true, but "Y" is always true. Your test should presumably instead be either

if answer == "y" or answer == "Y":
or...

if answer in ["y", "Y"]:
Reply
#3
(Jan-13-2021, 06:53 PM)bowlofred Wrote: This is a precedence issue. Your if statement is parsed as:

if (answer == "y") or ("Y"):
Answer == "y" may not be true, but "Y" is always true. Your test should presumably instead be either

if answer == "y" or answer == "Y":
THANK YOU!!!!
or...

if answer in ["y", "Y"]:
Reply
#4
Thank you!!!
Reply


Forum Jump:

User Panel Messages

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