229k views
0 votes
Write a calculator program that keeps track of a subtotal like real calculators do. Start by asking the user for an initial number. Inside a loop, asks the user for a mathematical operation and a second number. Do the operation with the first number on the second number. Keep track of the result as a 'subtotal' to be used as the first number when going back to the top of the loop. When the user quits, print out the final result and quit the program. See output for an example for the behavior of the program. The operations should be at least

1 Answer

5 votes

Answer:

num1 = float(input("Enter the first number: "))

while True:

print("Add, Subtract, Multiply, Divide, or Quit ?")

choice = input("Your choice is: ")

if choice == "Quit":

break

num2 = float(input("Enter the second number: "))

if choice == "Add":

num1 += + num2

print("The subtotal is: " + str(num1))

elif choice == "Subtract":

num1 -= num2

print("The subtotal is: " + str(num1))

elif choice == "Multiply":

num1 *= num2

print("The subtotal is: " + str(num1))

elif choice == "Divide":

num1 /= num2

print("The subtotal is: " + str(num1))

print("The final result is: " + str(num1))

Step-by-step explanation:

Ask the user for the first number

Create a while loop that iterates until specified condition occurs inside the loop

Ask the user for the operation or quitting

If the user chooses quitting, stop the loop. Otherwise, get the second number, perform the operation and print the subtotal

When the loop is done, the user enters Quit, print the final value result

User Mtzd
by
5.5k points