1

Just learning how to code, and wanted to make a small program to see what I know.

n = int(input("Pick a number any Number: "))
if  n > 100:
    print ("No... Not that Number")
else:
    answer = input("Would you like to know your number?")
    if answer == "Y" or "Yes" or "y" or "yes":
        print ("Your number is %s" % (n))
    elif answer == "N" or "No" or "n" or "no" or "NO":
        print ("Oh, well that's a shame then.")
    else:
        print ("Please type Yes or No")

input("Press Enter/Return to Exit")

Everything works, except for the second if statement, which doesn't follow any of the data entered into input. Any reason why it does this?

EBH
  • 10,350
  • 3
  • 34
  • 59

2 Answers2

1

Python isn't human, it doesn't understand

 if answer == "Y" or "Yes"

The way you mean it to. You should do

if answer == 'Y' or answer == 'Yes'

Or even better

if answer in ('Yes', 'Y', 'yes', 'y')

Or even shorter

if answer.lower() in ('yes', 'y')
Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62
0

== has a higher precedence than or. So, in the if condition your're checking whether answer == 'Y' and then oring this boolean expression with "Yes", which is a non-None string, so it evaluates as True. Instead, you should use the in operator to check if answer is one of the values you're interested in:

if answer in ("Y", "Yes", "y", "yes"):
    print ("Your number is %s" % (n))
elif answer in ("N", "No", "n", "no", "NO"):
    print ("Oh, well that's a shame then.")
else:
    print ("Please type Yes or No")
Mureinik
  • 297,002
  • 52
  • 306
  • 350