Answer to Question #226376 in Python for MANVITHA

Question #226376

Write a program to create a menu-driven calculator that performs arithmetic operations (+, -, *, /, and %).

The input will be a single line containing 2 integers and operator(+, -, *, /, and %) similar to 3 + 5.

If given operator is "+", print sum of two numbers.

"-", print subtraction of the two numbers.

"*", print multiplication of the two numbers.

"/", print division of the two numbers.

"%", print modulus operation of the two numbers.

Explanation

For EX, if the operator is "+" and two numbers are 3 and 5. As it is addition, your code should print the sum of two numbers (3 + 5), which is 8. Similarly, if it is "*" and two numbers are 2 and 5.

As it is a multiplication, your code should print result of multiplication of 2 numbers (2 * 5), which is 10.

Similarly, if operator is "-" & two numbers are 10 and 9.As it is subtraction operator, your code should print result of subtraction of 2 numbers (10 - 9), which is 1.





1
Expert's answer
2021-08-16T01:03:38-0400
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')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS