0

If the user doesn't type in multiplication, addition, ect, then a string should be printed. However, if one of these words is typed it still prints this string. I've just started with python; sorry that it's such a basic question!

print ("Multiplication, Addition, Subtraction or Division?")
type = str(input("Your choice:"))
if type != "multiplication" or "addition" or "subtraction" or "division":
    print("YOU DID NOT ENTER ONE OF THE ABOVE OPTIONS.\nTRY AGAIN!")
num1 = int(input("Enter your 1st number:"))
  • 2
    I’m no python programmer but, I thing you need to say type != “str” or type != “otherstr” – Pablo Recalde Jan 30 '21 at 15:43
  • I don't understand what you mean. I don't have a list of the words that should be accepted other than what I have posted here, in my code. –  Jan 30 '21 at 15:49
  • 1
    the function type() returns class type of the argument(object) passed as parameter, do not use it as a variable – Andrewgmz Jan 30 '21 at 15:57

2 Answers2

2
print ("Multiplication, Addition, Subtraction or Division?")
choice = input("Your choice:")
if choice.lower() not in ("multiplication", "addition", "subtraction", "division"):
    print("YOU DID NOT ENTER ONE OF THE ABOVE OPTIONS.\nTRY AGAIN!")
num1 = int(input("Enter your 1st number:"))

Also type is the built-in global name. By using this name you hide it.

4xy
  • 3,494
  • 2
  • 20
  • 35
0

The problem is on your if statement. When you are testing if a value is one of multiple options, you can use the in keyword like "test" in ["hello", "test", "123"] == true.

print ("Multiplication, Addition, Subtraction or Division?")
type = str(input("Your choice:"))

if type not in ["multiplication", "addition", "subtraction", "division"]:
    print("YOU DID NOT ENTER ONE OF THE ABOVE OPTIONS.")
    print("TRY AGAIN!")
Leo
  • 1,273
  • 9
  • 14