Skip to content

Instantly share code, notes, and snippets.

@piontekle
Created August 6, 2020 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piontekle/1ef1b363751ea75d8802674daf7139a4 to your computer and use it in GitHub Desktop.
Save piontekle/1ef1b363751ea75d8802674daf7139a4 to your computer and use it in GitHub Desktop.
Basic calculator built in Python3
import re
print("Welcome to The Calculator")
print("Type 'quit' to exit\n")
previous = 0
run = True
def perform_math():
global run
global previous
equation = ""
if previous == 0:
equation = input("Enter equation:")
else:
equation = input("Enter equation (or type clear for new equation): " + str(previous))
if equation == "quit":
print("Goodbye, human")
run = False
elif equation == "clear":
previous = 0
else:
equation = re.sub('[a-zA-z,.:()" "]', '', equation)
if previous == 0:
previous = eval(equation)
else:
previous = eval(str(previous) + equation)
while run:
perform_math()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment