0

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.

Community
  • 1
  • 1
Avi
  • 1
  • `I expect that if you enter a 1, 2, or 3 it will go into the else nest` If you input 1, `item_n != "1" or item_n != "2" or item_n != "3"` evaluates to `False or True or True` -> `True`. – tkausl Feb 08 '19 at 06:03
  • Change or to and. – Mad Physicist Feb 08 '19 at 06:04
  • 1
    Better yet, use `item_n not in ('1', '2', '3')`. Don't do `item_n not in '123'`. That will match `12`, `23` and `123` – Mad Physicist Feb 08 '19 at 06:06
  • I swapped my if and else statements and changed the "!=" to "==" and it seems to be working properly! I see what you mean though, thank you for your answer. I also am unsure how to mark this question as accepted. – Avi Feb 08 '19 at 06:26
  • Possible duplicate of [Python testing whether a string is one of a certain set of values](https://stackoverflow.com/questions/17902492/python-testing-whether-a-string-is-one-of-a-certain-set-of-values) – tripleee Feb 08 '19 at 06:30

3 Answers3

2

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

Kartik Gautam
  • 257
  • 3
  • 12
1

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

pL4Gu33
  • 2,045
  • 16
  • 38
  • I swapped my if and else statements and changed the "!=" to "==" and it seems to be working properly! I see what you mean though, thank you for your answer. I also am unsure how to mark this question as accepted. – Avi Feb 08 '19 at 06:21
0

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)"))