Avi
Avi

Reputation: 1

Why is my "If" statement being triggered?

This is my first post, so I apologize if I do anything incorrectly.

I am currently writing a simple program for a college class. The instructions for this specific part say:

The value of the entry MUST be “1”, or “2”, or “3”. There are different ways you can test for this.

You may create different validation techniques if you like as long as they work. If the entry is not valid, respond with an appropriate error message and re-prompt.

Now when I prompt the user to enter a selection (lets say they enter "1") it thinks it is an invalid input.

I personally think it should take the answer as an int value but the instructions say it should not. Am I missing something small here?

I have tried editing the code with different ' and " marks. I think I might be making a small syntax error but I can't put my finger on it.

cont= str("y")
cart = int(0)
item_total = int(0) 
order_total= float(0)

cont=input("Would you like to place an order? ")
while(cont.lower() == "y"):
  print("Order for John Doe")
  print("1. Savannah")
  print("2. Thin Mints")
  print("3. Tagalongs")
  item_n=input("Please choose a flavor ")
  if(item_n != "1" or item_n != "2" or item_n != "3"):
    print("Invalid entry, please try again")
  else:
    new_item=int(input("How many would you like (1-10)"))

I expect that if you enter a 1, 2, or 3 it will go into the else nest, but it does not. I can also post more of the professor's instructions if needed.

Upvotes: 0

Views: 124

Answers (3)

ask_andrew
ask_andrew

Reputation: 1

try this:

cont= str("y")
cart = int(0)
item_total = int(0)
order_total= float(0)

cont=input("Would you like to place an order? ")
while(cont.lower() == "y"):
  print("Order for John Doe")
  print("1. Savannah")
  print("2. Thin Mints")
  print("3. Tagalongs")
  item_n=input("Please choose a flavor ")

  if(item_n not in ["1","2","3"]):
    print("Invalid entry, please try again")
  else:
    new_item=int(input("How many would you like (1-10)"))

Upvotes: 0

Kartik Gautam
Kartik Gautam

Reputation: 257

you should use this

item_n=int(input("Please choose a flavor "))

instead of this

item_n=input("Please choose a flavor ")

as input function takes strings so you need to convert it into int

and use AND instead of OR in the if statement

Upvotes: 2

pL4Gu33
pL4Gu33

Reputation: 2085

You should use an AND not an OR. Because it should not 1 AND not 2 AND not 3 for an error.

Upvotes: 1

Related Questions