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))
2
  • 3
    Possible duplicate of How to test multiple variables against a value? Commented May 29, 2019 at 12:40
  • Start your Python, enter "2" == "1" or "add" and consider the result. Commented May 29, 2019 at 13:24

3 Answers 3

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.

answered May 29, 2019 at 12:40
Sign up to request clarification or add additional context in comments.

1 Comment

it is better to use operation in ["1","add"] rather than operation == "1" or operation == "add" .
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")
answered May 29, 2019 at 12:40

Comments

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))
answered May 29, 2019 at 12:40

2 Comments

it is better to use operation in ["1","add"] rather than operation == "1" or operation == "add" .
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.