Created
July 22, 2024 18:20
-
-
Save 3dgoose/ecd0b115ca1aed45feace2d9ebe35204 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from machine import Pin | |
import time | |
import math | |
memory = {} | |
print("MPY CALCULATING TERMINAL") | |
def calculate(expression): | |
try: | |
if '+' in expression: | |
operand1, operand2 = expression.split('+') | |
result = float(operand1) + float(operand2) | |
elif '-' in expression: | |
operand1, operand2 = expression.split('-') | |
result = float(operand1) - float(operand2) | |
elif '*' in expression: | |
operand1, operand2 = expression.split('*') | |
result = float(operand1) * float(operand2) | |
elif '/' in expression: | |
operand1, operand2 = expression.split('/') | |
result = float(operand1) / float(operand2) | |
elif '^' in expression: | |
operand1, operand2 = expression.split('^') | |
result = float(operand1) ** float(operand2) | |
elif 'sqrt' in expression: | |
_, operand = expression.split('sqrt') | |
result = math.sqrt(float(operand)) | |
else: | |
return "Invalid input" | |
return result | |
except ValueError: | |
return "Invalid input. Enter a valid command." | |
except ZeroDivisionError: | |
return "Division by zero is not allowed." | |
except Exception as e: | |
return "An error occurred: " + str(e) | |
while True: | |
user_input = input(">>> ").strip() | |
if user_input.lower() == "exit": | |
break | |
elif user_input.lower().startswith("store"): | |
_, key = user_input.split() | |
if 'result' in locals(): | |
memory[key] = result | |
print(f"Stored {result} in memory under key '{key}'") | |
else: | |
print("No result to store. Please perform a calculation first.") | |
elif user_input.lower().startswith("recall"): | |
_, key = user_input.split() | |
if key in memory: | |
print(f"Recalled value from key '{key}': {memory[key]}") | |
else: | |
print(f"No value stored under key '{key}'") | |
else: | |
result = calculate(user_input) | |
print("Result:", result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment