ETechSavvies

Untitled

Apr 27th, 2021 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. #import re module to work with regular expressions
  2. import re
  3.  
  4. run = True
  5. oldEq = 0
  6.  
  7. #this is our main function that does all the calculation
  8. def calc():
  9.     #we use global keyword to access the global variables which are declared outside the function
  10.     global run, oldEq
  11.     #we use to variables oldEq and newEq to be able to use the results we get as an expression for the next eval() execution
  12.     if oldEq == 0:
  13.         newEq = input("enter the equation\n")
  14.     else:
  15.         newEq = input(str(oldEq))
  16.     if newEq == 'quit':
  17.         run = False
  18.     else:
  19.         #this remove all letters and punctuations specified in our pattern and make our expression composed of only numbers
  20.         newEq = re.sub('[a-zA-Z,./" "]', '', newEq)
  21.         if oldEq == 0:
  22.             oldEq = eval(newEq)
  23.         else:
  24.             oldEq = eval(str(oldEq) + newEq)
  25.         print("=", oldEq)
  26. #this will check if the run variable is set to True to continue calculating or stop the program incase of run = False
  27. while run:
  28.     calc()
Add Comment
Please, Sign In to add comment