if elif is giving the wrong result
Question:
It’s only giving me the result of the first operation wich is multiply even if my input was addition
print('Calculator')
operation = input('Choose:n Mltiply(*)n Divise(/)n Add(+)n Subbstract(-)n ')
if operation == 'mutiply' or 'Multiply' or 'MULTIPLY' or '*':
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) * float(num2)
print(result )
elif operation == 'DIVISE' or 'divise' or 'Divise' or '/':
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) / float(num2)
print(result )
elif operation == 'Add' or 'add' or 'ADD' or '+':
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) + float(num2)
print(result )
elif operation == 'Subbstract' or 'subbstract' or 'SUBBSTRACT' or '-':
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) - float(num2)
print(result )
else:
print('SYNTAX ERROR')
Answers:
Use the in operator
elif operation in {'DIVISE', 'divise', 'Divise', '/'}:
for every if/else block.
EDIT:
You can find a good explanation here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
Use in
print('Calculator')
operation = input('Choose:n Mltiply(*)n Divise(/)n Add(+)n Subbstract(-)n ')
if operation.lower() in ['mutiply', '*']:
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) * float(num2)
print(result )
elif operation.lower() in ['mutiply', '*']:
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) / float(num2)
print(result )
..... replicate the above code for other operations ....
Why is your code always meet the fist condition?
operation == 'mutiply' or 'Multiply' or 'MULTIPLY' or '*'
this always gives you True
,
if 'Multiply':
# Always return true
It’s only giving me the result of the first operation wich is multiply even if my input was addition
print('Calculator')
operation = input('Choose:n Mltiply(*)n Divise(/)n Add(+)n Subbstract(-)n ')
if operation == 'mutiply' or 'Multiply' or 'MULTIPLY' or '*':
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) * float(num2)
print(result )
elif operation == 'DIVISE' or 'divise' or 'Divise' or '/':
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) / float(num2)
print(result )
elif operation == 'Add' or 'add' or 'ADD' or '+':
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) + float(num2)
print(result )
elif operation == 'Subbstract' or 'subbstract' or 'SUBBSTRACT' or '-':
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) - float(num2)
print(result )
else:
print('SYNTAX ERROR')
Use the in operator
elif operation in {'DIVISE', 'divise', 'Divise', '/'}:
for every if/else block.
EDIT:
You can find a good explanation here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
Use in
print('Calculator')
operation = input('Choose:n Mltiply(*)n Divise(/)n Add(+)n Subbstract(-)n ')
if operation.lower() in ['mutiply', '*']:
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) * float(num2)
print(result )
elif operation.lower() in ['mutiply', '*']:
num1 = input('Write Your number :')
num2 = input('Write your next number :')
result = float(num1) / float(num2)
print(result )
..... replicate the above code for other operations ....
Why is your code always meet the fist condition?
operation == 'mutiply' or 'Multiply' or 'MULTIPLY' or '*'
this always gives you True
,
if 'Multiply':
# Always return true