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
6 ответов
+ 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.
+ 9
Simply, you should tweak the code:
if oper == 'x' or oper == '*':
+ 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')
0
okay I get it now thank you very much
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.
0
okay I changed it works now thank you