Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python3
import math
import collections
import types
def main():
quit = "Ctrl+Z, Enter"
prompt = "Enter an expression ({} to quit): ".format(quit)
current = types.SimpleNamespace(letter="A")
globalContent = global_context()
localContext = collections.OrderedDict()
while True:
try:
expression = input(prompt)
if expression:
calculate(expression, globalContent, localContext, current)
except EOFError:
print()
break
def global_context():
print(dir(math))
globalContent = globals().copy()
for name in dir(math):
if not name.startswith("_"):
globalContent[name] = getattr(math, name)
return globalContent
def calculate(expression, globalContent, localContext, current):
try:
result = eval(expression, globalContent, localContext)
update(localContext, result, current)
print(", ".join(["{}={}".format(variable, value)
for variable, value in localContext.items()])
)
print("ANS={}".format(result))
except Exception as e:
print(e)
def update(localContext, result, current):
localContext[current.letter] = result
current.letter = chr(ord(current.letter) + 1)
if current.letter > "Z":
current.letter = "A"
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment