calc = []
#record history
def record_history(of_calc):
calc.append(of_calc)
#history
def history():
if calc == []:
print("No past calculations to show")
else:
for a in calc:
print(a)
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 )
#recall record history with the saved last calc
record_history(last_calculation)
elif choice == ('?'):
history()
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("8.History : ? ")
# take input from the user
choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ")
print(choice)
if(select_op(choice) == -1):
#program ends here
print("Done. Terminating")
break
Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): -
Enter first number: 4 4 Enter second number: 5 5 4.0 - 5.0 = -1.0 Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): + +* Enter first number: 5 5 Enter second number: 7 7 5.0 + 7.0 = 12.0 Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,,/,^,%,#,$,?): + + Enter first number: 3.4 3.4 Enter second number: 5.6 5.6 3.4 + 5.6 = 9.0 Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,*,/,^,%,#,$,?): -
Enter first number: 6.66 6.66 Enter second number: 5.66 5.66 6.66 - 5.66 = 1.0 Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,,/,^,%,#,$,?): ? ? 4.0 - 5.0 = -1.0 5.0 + 7.0 = 12.0 3.4 + 5.6 = 9.0 6.66 - 5.66 = 1.0 Select operation. 1.Add : + 2.Subtract : - 3.Multiply : * 4.Divide : / 5.Power : ^ 6.Remainder: % 7.Terminate: # 8.Reset : $ 8.History : ? Enter choice(+,-,,/,^,%,#,$,?):