Can someone help me if someone puts in the wrong operator is still registers as multiplication. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone help me if someone puts in the wrong operator is still registers as multiplication.

try: num1 = float(input('Enter number')) except: print('Invalid input') oper = input('-|+|x|/|') try: num2 = float(input('Enter number')) except: print('inavalid input') if oper == ('-'): result = num1 - num2 elif oper == ('+'): result = num1 + num2 elif oper == ('/'): result = num1 / num2 elif oper == ('x') or oper == ('*'): result = num1 * num2 else: print('invalid operator') print(result) https://code.sololearn.com/cSbaV08ZnwE1/?ref=app

8th Sep 2017, 4:46 AM
Darius Robinson
Darius Robinson - avatar
6 Answers
+ 2
Firstly test1 and test2 are never assigned which would throw an error. But that's not what you're asking about you're asking about why (oper == 'x' or '*') always return true, that's because it converts your '*' into true.
8th Sep 2017, 5:23 AM
Marcus Søndergaard
Marcus Søndergaard - avatar
+ 9
Simply, you should tweak the code: if oper == 'x' or oper == '*':
8th Sep 2017, 5:31 AM
Hatsy Rei
Hatsy Rei - avatar
+ 9
It doesn't seem like test1 and test2 were ever used in your program. You could have just done: if oper == ('-'): result = num1 - num2 elif oper == ('+'): result = num1 + num2 elif oper == ('/'): result = num1 / num2 elif oper == ('x') or oper == ('*'): result = num1 * num2 else: print('invalid operator')
8th Sep 2017, 5:57 AM
Hatsy Rei
Hatsy Rei - avatar
0
okay I get it now thank you very much
8th Sep 2017, 5:54 AM
Darius Robinson
Darius Robinson - avatar
0
okay I see what you mean. I thought learning this will be easy but it's a lot of tricky stuff in here. so I'm going to be asking you guys for a lot on help. It will be greatly appreciated.
8th Sep 2017, 6:04 AM
Darius Robinson
Darius Robinson - avatar
0
okay I changed it works now thank you
8th Sep 2017, 6:08 AM
Darius Robinson
Darius Robinson - avatar