Commoq
Commoq

Reputation: 15

How to check that a string is equal to one of two strings in Python

This is my code:

print("What is your Name")
user_name = input("User Name: ")
print(f"Hello {user_name} please choose a dish and a drink from this menu : \n Fish \t Eggs \n Water \t Juice")
food = input("Please input your desired dish: ")
drink = input("Please input your desired drink: ")
if food != "Fish" or "Eggs":
  print("Please input a correct dish or drink")
else:
  print(f"{user_name} your desired drink is {drink} and your desired dish is {food}")

The main problem is the final part. I'm trying to say "if food is not equal to Fish or Eggs print the error message but if it is print the success message". But, if you copy the code and follow it at the end it always prints the error message.

Upvotes: -1

Views: 710

Answers (3)

Bibhav
Bibhav

Reputation: 1747

Code:

print("What is your Name")
user_name = input("User Name: ")
print(f"Hello {user_name} please choose a dish and a drink from this menu : \n Fish \t Eggs \n Water \t Juice")
food = input("Please input your desired dish: ")
drink = input("Please input your desired drink: ")
if food not in ("Fish","Eggs"):
  print("Please input a correct dish or drink")
else:
  print(f"{user_name} your desired drink is {drink} and your desired dish is {food}")
  • You must either write if food !="Fish" and food !="Eggs": or if food not in ("Fish","Eggs"):

Upvotes: 4

Nalq
Nalq

Reputation: 14

if food != "Fish" or "Eggs":

your input will always make one of the above condition true, because you have option to input either Fish or Eggs, so instead of or you need to use and condition with explicit check for both items to satisfy your condition.

if food != "Fish" and food != "Eggs":

Upvotes: -2

Benjamin4991
Benjamin4991

Reputation: 163

You could do if food not in ["Fish", "Eggs"].

The problem is, you are evaluating food != "Fish" and "Eggs". The latter evaluates to True in a boolean context. Therefore, the whole statement is evaluated to True.

Upvotes: 2

Related Questions