why conditional "if" is looping for first and second line even it is false | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why conditional "if" is looping for first and second line even it is false

# calculator calculate = input("Which operation you want to do? Press '+' for addition, '-' for substaction, '*' for multiplication and '/' for division: ") if calculate == "+" or "-" or "*" or "/": a=float(input("First number: ")) b=float(input("Second number: ")) if calculate == "+": print(a+b) elif calculate == "-": print(a-b) elif calculate == "*": print(a*b) else: print(a/b) else: print("Invalid Operator")

29th Jul 2020, 2:56 PM
Abhishek Kumar
Abhishek Kumar - avatar
3 Answers
+ 10
The if statement should be: if calculate == "+" or calculate == "-" or calculate == "*" or calculate == "/": but you better use: if calculate in ['+','-','*','/']:
29th Jul 2020, 3:01 PM
Lothar
Lothar - avatar
+ 2
This is the Boolean logic On the first line: "+" or "-" or "*" or "/" always evaluates to True, You don't want that, you want: something == another or something == something else
29th Jul 2020, 3:01 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 1
Ali Abdelhady Lothar thank you bros...it really help me. 😊
29th Jul 2020, 5:30 PM
Abhishek Kumar
Abhishek Kumar - avatar