Created
August 6, 2020 14:55
-
-
Save piontekle/1ef1b363751ea75d8802674daf7139a4 to your computer and use it in GitHub Desktop.
Basic calculator built in Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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