0

I can't get my head around this particular if..else statement:

print("Do you want to run the next bit of code? Y/N")
n = input()

if n == "Y" or "y":
    j = [12, 43, 54, 65]
    for i in j:
        print (i, end="" "\t")

elif n == "N" or "n":
    print("You get numbers if you had pressed Y")

else:
    print("That's not an option")

My problem here is, whatever the value I give for n, it always gives me the output of Y or y. I always get the output as the array numbers. Which means that my condition is not working. So what is actually the problem with the first condition?

Ewan
  • 14,592
  • 6
  • 48
  • 62
zuestech
  • 93
  • 1
  • 6
  • 4
    you need to do `if n == "Y" or n == "y"` otherwise "y" evaluates always to True. Alternatively you can do something like: `if n in ("Y", "y")` which is more compact and allows you to put a large number of characters to be checked –  Nov 06 '19 at 12:22
  • @SembeiNorimaki any particular reason why you used a tuple? It works equally well with a list. Thanks. – Neeraj Hanumante Nov 06 '19 at 12:29
  • you can write if n.lower() =="y" and same for elif condition. it will work – Sanket Patel Nov 06 '19 at 12:29
  • @Neeraj no particular reason. A list works also fine, both options are correct –  Nov 06 '19 at 12:33

1 Answers1

0

Check this. The following code works.

print("Do you want to run the next bit of code? Y/N")
n = input()
if n in ["Y", "y"]:
    j = [12, 43, 54, 65]
    for i in j:
        print (i, end="" "\t")
elif n in ["N", "n"]:
    print("You get numbers if you had pressed Y")
else:
    print("That's not an option")
Neeraj Hanumante
  • 1,575
  • 2
  • 18
  • 37