Write a program to create a menu-driven calculator that performs basic arithmetic operations (+, -, *, /, and %).Input
The input will be a single line containing two integers and operator(+, -, *, /, and %) similar to 3 + 5.Output
If the given operator is "+", print the sum of two numbers.
If the given operator is "-", print the result of the subtraction of the two numbers.
If the given operator is "*", print the multiplication of the two numbers.
For example, if the given operator is "+" and the two numbers are 3 and 5. As it is an addition operator, your code should print the sum of the given two numbers (3 + 5), which is 8.
Similarly, if the given operator is "*" and the two numbers are 2 and 5.
Similarly, if the given operator is "-" and the two numbers are 10 and 9.
Sample Input 1
3 + 5
Sample Output 1
8
Sample Input 2
2 * 5
Sample Output 2
input will be single line only
str = input('Please enter a math expression: ')
number1 = ''
number2 = ''
operation = ''
for i in range(len(str)):
if not str[i] in ['+', '-', '*', '/', '%']:
number1 += str[i]
else:
operation = str[i]
number2 = str[i + 1:len(str)]
break
if operation == "+":
print(int(number1) + int(number2))
elif operation == '-':
print(int(number1) - int(number2))
elif operation == '*':
print(int(number1) * int(number2))
elif operation == '/':
print(int(number1) / int(number2))
elif operation == '%':
print(int(number1) % int(number2))
else:
print('Error')
Comments
Leave a comment