Calculator v2.0 - python
Target : Add History Option and make more Accurate
In this case I was trying to add a option which can remember past operations and return them when we want with full details ;such as our inputs, operation we done and result.
first of all I recoded option called "RESET" which we can rest our calculations rest our process as we can reset our process either after we select the operation and either while we entered onr number also.
i used a while loop and add all the operators in to it as only run program while return is true, then coded for "terminate " option and "reset option" cause some times we maybe have to reset all in this stage too.
def select_op(choice):
if (choice == '#'):
return -1
elif (choice == '$'):
return 0
and when return -1 for choice its terminating and exiting from program
if(select_op(choice) == -1):
#program ends here
print("Done. Terminating")
exit()
Also after that added few more lines in Oder to can reset while entering numbers too..
elif (choice in ('+','-','*','/','^','%')):
while (True):
num1s = str(input("Enter first number: "))
print(num1s)
if num1s.endswith('$'):
return 0
if num1s.endswith('#'):
return -1
(Note: I refer geekforgeeks.org and W3 schools for this .endswith commands)
Then I Extended the functional of the calculator to save and display the past results of the arithmetic operations
I list an list to record all the operations but in case i have to face lot of issues bcz of data type matters;
then I define variable for assign each operation when it running and another variable to assign the string value of that last calculation , then I recorded that string in a list by using append() command;
last_calculation = "{0} {1} {2} = {3}".format(num1, choice, num2, result)
print(last_calculation )
#asign values to history list
his_string=str(last_calculation)
history_list.append(his_string)
here how I tested this function before adding to main code;
Then defined a function as history according to do calculations what we have in history option;
In here I also coded a few lines to output when there are no past operations done whenever u calling for history option
def history():
if len(history_list)==0:
print ("No past calculations to show")
else:
for y in history_list:
print(str(y))
after Few Adjustments I got something like this ; )
Here is my Source Code ; )
history_list=[]
#defining fuctions for each option
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 history():
if len(history_list)==0:
print ("No past calculations to show")
else:
for y in history_list:
print(str(y))
def select_op(choice):
if (choice == '#'):
return -1
elif (choice == '$'):
return 0
elif (choice=='?'):
history()
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)
##elif choice=='?':
#print("testing history will here")
else:
print("Something Went Wrong")
last_calculation = "{0} {1} {2} = {3}".format(num1, choice, num2, result)
print(last_calculation )
#asign values to history list
his_string=str(last_calculation)
history_list.append(his_string)
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")
exit()
Comments
Post a Comment