0

I'm trying to learn Python by making simple programs. One idea I had was to create a program which loops as long as conditions are not met. Here is the code:


print("What would you like to do?")
action = input("a. eat it   b. cook it  c. toss it\n>").lower()
while action != "a" or "b" or "c":
    print("That is not one of your options.")
    action = input(">")
if action == "a":
    print("You eat it.")
elif action == "b":
    print("You cook it.")
elif action == "c":
    print("You throw it away.")

It's supposed to continue the loop and reask for a response as long as a, b, or c are not entered. The problem is that even after I enter a, b, or c, it still tells me that that it's not one of the options (that is, it still remains in the while loop even though the conditions for exiting the loop seem to be met.) Why is action not being assigned the value that I input so as to exit the loop?

Syferion
  • 5
  • 4
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – John Gordon May 20 '20 at 19:26

3 Answers3

1

You need to use and instead of or. The or operator evaluates to true if any of the conditions are met. So if you choose a, it still is true, because it wasn't b or c.

Try this:

print("What would you like to do?")
action = input("a. eat it   b. cook it  c. toss it\n>").lower()
while action != "a" and action!= "b" and action!="c":
    print("That is not one of your options.")
    action = input(">")
if action == "a":
    print("You eat it.")
elif action == "b":
    print("You cook it.")
elif action == "c":
    print("You throw it away.")
Brenden Price
  • 517
  • 2
  • 9
0

The way you are making the condition action != "a" or "b" or "c": in the while loop, it will always be True. Because "b", "c" will be evaluated as True.

You need to change the condition as follows and need to replace or by and as well:

action != "a" and action != "b" and action != "c":

Another way to do is following:

"abc".find(action) != -1 and len(action) == 1:

Another way can be the following:

action not in ["a", "b", "c"]:
Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
0

The test you're doing is never going to return False because both "b" and "c" are seen as "truthy" values, and you're saying "while action is not a OR ("b" or "c" is true)"--probably not what you're trying to express.

You can express your condition more accurately as:

while action not in ("a", "b", "c"):
    ...

In addition, this is the sort of thing where a dict is a great approach. Can you think of a way to store both the valid commands and the responses they cause in a dictionary?

asthasr
  • 9,125
  • 1
  • 29
  • 43
  • Thank you for the idea. Would you mind explaining how a dictionary would work with this? To a beginner, that doesn't sound very simple (but I'd like to learn!). – Syferion May 20 '20 at 19:49
  • You are basically mapping a choice (a, b, c...) to an output (You eat it, ...). Dictionaries are "mappings" from one value to another. In addition you can check for membership in a dictionary easily with `in`; or you can simply catch `KeyError` if a given choice doesn't exist. – asthasr May 20 '20 at 23:04