For the below code I need to add a history list with below conditions, Objectives Extend the functionali of the the calculator to save and display the past results of the arithmetic operations Stage 2: Save and display calculation history of the calculator In this second stage of the assignment you will extend the given calculator program to record the calculations, and recall them as a list using an additional command '?'. Task 1: Study the given code in the answer box and extend it to save each executed operation in a Python List. Declare a list to store the previous operations Save the operator, operands and the results as a single string, for each operation after each calculation Task 2: implement a history() function to handle the operation '?' Display the complete saved list of operations (in the order of execution) using a new command ‘?’ If there are no previous calculations when the history '?' command is used, you can display the following message "No past calculations to show" Code: def add(a,b): return a+b def subtract(a,b): return a-b def multiply (a,b): return a*b def divide(a,b): try: return a/b except Exception as e: print(e) def power(a,b): return a**b def remainder(a,b): return a%b def select_op(choice): if (choice == '#'): return -1 elif (choice == '$'): return 0 elif (choice in ('+','-','*','/','^','%')): while (True): num1s = str(input("Enter first number: ")) print(num1s) if num1s.endswith('$'): return 0 if num1s.endswith('#'): return -1 try: num1 = float(num1s) break except: print("Not a valid number,please enter again") continue while (True): num2s = str(input("Enter second number: ")) print(num2s) if num2s.endswith('$'): return 0 if num2s.endswith('#'): return -1 try: num2 = float(num2s) break except: print("Not a valid number,please enter again") continue result = 0.0 last_calculation = "" if choice == '+': result = add(num1, num2) elif choice == '-': result = subtract(num1, num2) elif choice == '*': result = multiply(num1, num2) elif choice == '/': result = divide(num1, num2) elif choice == '^': result = power(num1, num2) elif choice == '%': result = remainder(num1, num2) else: print("Something Went Wrong") last_calculation = "{0} {1} {2} = {3}".format(num1, choice, num2, result) print(last_calculation ) else: print("Unrecognized operation") while True: print("Select operation.") print("1.Add : + ") print("2.Subtract : - ") print("3.Multiply : * ") print("4.Divide : / ") print("5.Power : ^ ") print("6.Remainder: % ") print("7.Terminate: # ") print("8.Reset : $ ") print("9.History : ? ") # take input from the user choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ") print(choice) if(select_op(choice) == -1): #program ends here print("Done. Terminating") exit()
For the below code I need to add a history list with below conditions,
Objectives
-
Extend the functionali of the the calculator to save and display the past results of the arithmetic operations
Stage 2: Save and display calculation history of the calculator
In this second stage of the assignment you will extend the given calculator program to record the calculations, and recall them as a list using an additional command '?'.
Task 1: Study the given code in the answer box and extend it to save each executed operation in a Python List.
- Declare a list to store the previous operations
- Save the operator, operands and the results as a single string, for each operation after each calculation
Task 2: implement a history() function to handle the operation '?'
- Display the complete saved list of operations (in the order of execution) using a new command ‘?’
- If there are no previous calculations when the history '?' command is used, you can display the following message "No past calculations to show"
Code:
def add(a,b):
return a+b
def subtract(a,b):
return a-b
def multiply (a,b):
return a*b
def divide(a,b):
try:
return a/b
except Exception as e:
print(e)
def power(a,b):
return a**b
def remainder(a,b):
return a%b
def select_op(choice):
if (choice == '#'):
return -1
elif (choice == '$'):
return 0
elif (choice in ('+','-','*','/','^','%')):
while (True):
num1s = str(input("Enter first number: "))
print(num1s)
if num1s.endswith('$'):
return 0
if num1s.endswith('#'):
return -1
try:
num1 = float(num1s)
break
except:
print("Not a valid number,please enter again")
continue
while (True):
num2s = str(input("Enter second number: "))
print(num2s)
if num2s.endswith('$'):
return 0
if num2s.endswith('#'):
return -1
try:
num2 = float(num2s)
break
except:
print("Not a valid number,please enter again")
continue
result = 0.0
last_calculation = ""
if choice == '+':
result = add(num1, num2)
elif choice == '-':
result = subtract(num1, num2)
elif choice == '*':
result = multiply(num1, num2)
elif choice == '/':
result = divide(num1, num2)
elif choice == '^':
result = power(num1, num2)
elif choice == '%':
result = remainder(num1, num2)
else:
print("Something Went Wrong")
last_calculation = "{0} {1} {2} = {3}".format(num1, choice, num2, result)
print(last_calculation )
else:
print("Unrecognized operation")
while True:
print("Select operation.")
print("1.Add : + ")
print("2.Subtract : - ")
print("3.Multiply : * ")
print("4.Divide : / ")
print("5.Power : ^ ")
print("6.Remainder: % ")
print("7.Terminate: # ")
print("8.Reset : $ ")
print("9.History : ? ")
# take input from the user
choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ")
print(choice)
if(select_op(choice) == -1):
#program ends here
print("Done. Terminating")
exit()
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)