160k views
2 votes
Write a program to simulate a simple calculator. In your program ask the user to enter an operation selected from the menu, ask for two operands. Calculate the result and display it on the screen. Here is a sample run

User OverLex
by
7.0k points

1 Answer

5 votes

Answer:

Written in Python:

opcode = input("Enter operator: +,-,*,/")

operand1 = float(input("Operand 1: "))

operand2 = float(input("Operand 2: "))

if opcode == "+":

print(operand1 + operand2)

elif opcode == "-":

print(operand1 - operand2)

elif opcode == "*":

print(operand1 * operand2)

elif opcode == "/":

print(operand1 / operand2)

else:

print("Invalid Operator")

Step-by-step explanation:

This prompts user for operator

opcode = input("Enter operator: +,-,*,/")

The next two lines prompt user for two operands

operand1 = float(input("Operand 1: "))

operand2 = float(input("Operand 2: "))

This performs + operation if operator is +

if opcode == "+":

print(operand1 + operand2)

This performs - operation if operator is -

elif opcode == "-":

print(operand1 - operand2)

This performs * operation if operator is *

elif opcode == "*":

print(operand1 * operand2)

This performs / operation if operator is /

elif opcode == "/":

print(operand1 / operand2)

This displays invailid operator if input operator is not +,-,* or /

else:

print("Invalid Operator")

User Nam Bui
by
7.4k points