0

I want to program it like that so while taking input of num1,num2 and operation if user doesn't give input in appropriate type it ask the user again for input.

operation=(input('1.add\n2.subtract\n3.multiply\n4.divide'))
num1 =int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

if operation == "add" or operation == '1' :
   print(num1,"+",num2,"=", (num1+num2))
elif operation =="subtract" or operation == '2':
   print(num1,"-",num2,"=", (num1-num2))
elif operation =="multiply" or operation == '3':
   print(num1,"*",num2,"=", (num1*num2))
elif operation =="divide" or operation == '4':
   print(num1,"/",num2,"=", (num1/num2))
  • 3
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Sayse May 29 '19 at 12:40
  • Start your Python, enter `"2" == "1" or "add"` and consider the result. – molbdnilo May 29 '19 at 13:24

3 Answers3

1

You can use in keyword.

Ex:

>>> "1" in ["1","add"]
True
>>> "add" in ["1","add"]
True

Modify code like:

 operation=(input('1.add\n2.subtract\n3.multiply\n4.divide'))

    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))

    if operation in ["1","add"] :
       print(num1,"+",num2,"=", (num1+num2))
    elif operationi in ["2", "subtract"]:
       print(num1,"-",num2,"=", (num1-num2))
    elif operation in ["3", "multiply"]:
       print(num1,"*",num2,"=", (num1*num2))
    elif operation in ["4", "divide"]:
       print(num1,"/",num2,"=", (num1/num2))
    else:
        print("Invalid Input")
Harsha Biyani
  • 7,049
  • 9
  • 37
  • 61
1

Try this,

 operation=(input('1.add\n2.subtract\n3.multiply\n4.divide'))

    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))

    if operation == "1" or operation == "add" :
       print(num1,"+",num2,"=", (num1+num2))
    elif operation == "2" or operation == "subtract":
       print(num1,"-",num2,"=", (num1-num2))
    elif operation == "3" or operation == "multiply":
       print(num1,"*",num2,"=", (num1*num2))
    elif operation == "4" or operation == "divide":
       print(num1,"/",num2,"=", (num1/num2))
    else:
        print("Invalid Input")

Explanation:

In your code, IF will check condition-1 or condition-2 are True or False

if operation == "1" or operation == "add" 

here,

condition-1: operation == "1"

condition-2: operation == "add"

if operation == "1" or operation == "add" 

here,

condition-1: operation == "1"

condition-2: "add" # Always True as string contains elements.

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
0

This code is valid because in Python values are either truthy and falsy. However, the syntax for multicondition if clause is wrong. It should be if a == something and b == anotherthing.

So it will be as follows.

if operation == "1" or operation == "add" :
    print(num1,"+",num2,"=", (num1+num2))
Mansur
  • 1,661
  • 3
  • 17
  • 41
  • it is better to use `operation in ["1","add"]` rather than `operation == "1" or operation == "add" `. – Harsha Biyani May 29 '19 at 12:48
  • 2
    I know, however, the requester didn't ask for it. He said he cannot use 2 statements, that's why I mentioned this way. Nevertheless, you're absolutely right. – Mansur May 29 '19 at 12:56