@perfingo is not very explicit enough that eval() could be dangerous, so be careful when using it
as an alternative to avoid using huge if/elif/else block is to use operator module
as an alternative to avoid using huge if/elif/else block is to use operator module
from operator import add, mul, sub, truediv ops = {'+':add, 'plus':add, '-':sub, 'minus':sub, "multiplication":mul, "*":mul, "/":truediv, "divide":truediv} num1 = float(input ("Insert number: ")) operator = input ("What operation do you want to do: ").lower() num2 = float(input ("And the second number: ")) try: print(f'result: {ops[operator](num1, num2)}') except KeyError: print("Sorry, operator not identified, please try again")output(multiple runs)
Output:Insert number: 5
What operation do you want to do: +
And the second number: 4
result: 9.0
Output:Insert number: 5
What operation do you want to do: minus
And the second number: 3
result: 2.0
Output:Insert number: 4
What operation do you want to do: %
And the second number: 3
Sorry, operator not identified, please try again
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs