Forums

Always prints "Not an option"

I'm making code for a vending machine yet no matter what I put in it will print what it dispenses then "That's not an option". What code do I use?

Here's what I have right now.

if y == str("A1"):
    print("Here's your cheetos!")
if y == str("B1"):
    print("Here's your Coca-cola!")
if y == str("C1"):
    print("Here's your Gatorade!")
if y == str("D1"):
    print("Enjoy your Reese's!")
if y != str("A1") or str("B1") or str("C1") or str("D1"):
    print("That's not an option")

[edit by admin: formatting]

how do you get y?

y = input("Please make a selection: ")

I think the problem is in your second-last line. You have:

if y != str("A1") or str("B1") or str("C1") or str("D1"):
    print("That's not an option")

I think what you meant to say was:

if y != str("A1") and y != str("B1") and y != str("C1") and y != str("D1"):
    print("That's not an option")

The syntax that you originally had means "if y is not equal to the string "A1", or if the string "B1" is not empty, or if the string "C1" is not empty, or if the string "D1" is not empty, then..." In other words, the "or" bits separate out completely independent conditions -- they don't give a list of options for y.

A few more pointers on how to make your code better:

  • There's no need to wrap your strings in str. The code "A1" is actually identical to str("A1").
  • You can make the code much simpler by using the else and elif keywords.

As this looks like it's a learning assignment of some kind, I won't give you the details, because I'm sure you'd learn much more by playing with the code yourself and seeing what works :-)