-1

I'm currently working on a task and I'm finding that my ELIF statement is not working when I input my values. My inputs for each task are locationchoice = 2 and userchoice = 1.

if locationchoice == "1" or "America" and userchoice == "1":
  print("Yes This worked")
elif locationchoice == "2" or "Italy" and userchoice == "1":
  print("This worked too")

Every time I run this it always returns "Yes This worked" even with the locationchoice being 2. I might be dumb but any help?

Dan Lenski
  • 72,793
  • 12
  • 71
  • 117

1 Answers1

1

or separates booleans, so you're testing if "America" is true

either try this:

if locationchoice == "1" or locationchoice == "America" and userchoice == "1":
  print("Yes This worked")
elif locationchoice == "2" or locationchoice == "Italy" and userchoice == "1":
  print("This worked too")

or this:

if locationchoice in ["1", "America"] and userchoice == "1":
  print("Yes This worked")
elif locationchoice in ["2", "Italy"] and userchoice == "1":
  print("This worked too")

(i personally find the second option more readable which is why i added it in there)

Pwuurple
  • 312
  • 1
  • 10
  • Or, a bit more idiomatically, `if locationchoice in ("1", "America") and userchoice == "1":` – Dan Lenski Jun 03 '22 at 01:27
  • haha! i literally just added that before i even saw your comment! I find it much more appealing to read, though is it faster to use a list or tuple? – Pwuurple Jun 03 '22 at 01:28
  • It is faster to use a tuple (see [this comment](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true#comment109380603_20002504)), but that's a micro-optimization in most cases and you should worry about the clarity of the code rather than the performance of this. – Dan Lenski Jun 03 '22 at 01:49