Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import re module to work with regular expressions
- import re
- run = True
- oldEq = 0
- #this is our main function that does all the calculation
- def calc():
- #we use global keyword to access the global variables which are declared outside the function
- global run, oldEq
- #we use to variables oldEq and newEq to be able to use the results we get as an expression for the next eval() execution
- if oldEq == 0:
- newEq = input("enter the equation\n")
- else:
- newEq = input(str(oldEq))
- if newEq == 'quit':
- run = False
- else:
- #this remove all letters and punctuations specified in our pattern and make our expression composed of only numbers
- newEq = re.sub('[a-zA-Z,./" "]', '', newEq)
- if oldEq == 0:
- oldEq = eval(newEq)
- else:
- oldEq = eval(str(oldEq) + newEq)
- print("=", oldEq)
- #this will check if the run variable is set to True to continue calculating or stop the program incase of run = False
- while run:
- calc()
Add Comment
Please, Sign In to add comment