Skip to content

Instantly share code, notes, and snippets.

@vitalizzare
Last active May 25, 2021 22:01
Show Gist options
  • Save vitalizzare/761e2a58edb740145b25d87081d3ca92 to your computer and use it in GitHub Desktop.
Save vitalizzare/761e2a58edb740145b25d87081d3ca92 to your computer and use it in GitHub Desktop.
An alternative Winetoxin's Simple Calculator
# https://replit.com/@winetoxin/Winetoxins-Simple-Calculator
from operator import add, sub, mul, truediv
operations = {"+": add, "-": sub, "*": mul, "/": truediv}
intro = "Welcome to the Winetoxin's Python Project!!!"
decoration = '-+-'
border = decoration * (len(intro)//len(decoration)) + decoration[:len(intro)%len(decoration)]
intro = '\n'.join([border, intro, border])
logo = """
_____________________
| _________________ |
| | Pythonista 0. | | .----------------. .----------------. .----------------. .----------------.
| |_________________| | | .--------------. || .--------------. || .--------------. || .--------------. |
| ___ ___ ___ ___ | | | ______ | || | __ | || | _____ | || | ______ | |
| | 7 | 8 | 9 | | + | | | | .' ___ | | || | / \ | || | |_ _| | || | .' ___ | | |
| |___|___|___| |___| | | | / .' \_| | || | / /\ \ | || | | | | || | / .' \_| | |
| | 4 | 5 | 6 | | - | | | | | | | || | / ____ \ | || | | | _ | || | | | | |
| |___|___|___| |___| | | | \ `.___.'\ | || | _/ / \ \_ | || | _| |__/ | | || | \ `.___.'\ | |
| | 1 | 2 | 3 | | x | | | | `._____.' | || ||____| |____|| || | |________| | || | `._____.' | |
| |___|___|___| |___| | | | | || | | || | | || | | |
| | . | 0 | = | | / | | | '--------------' || '--------------' || '--------------' || '--------------' |
| |___|___|___| |___| | '----------------' '----------------' '----------------' '----------------'
|_____________________|
"""
def calculator(x=None, y=None):
# INIT
if x is None:
# TODO: write getfloat(prompt=None)->float, check exceptions
x = float(input("What is the first number: "))
else:
print(f"The first number is {x}")
if y is None:
y = float(input("What is the second number: "))
else:
print(f"The second number is {y}")
print(' '.join(operations.keys()))
while True:
operation_symbol = input("Pick an operation: ")
if operation_symbol in operations:
break
calculation_function = operations[operation_symbol]
# CALC
try:
answer = calculation_function(x, y)
except Exception as e:
print('ERROR:', repr(e))
answer = None
# PRESENT
print(f"{x} {operation_symbol} {y} = {answer}")
return answer
def main():
# INTRO
print(logo)
print(intro)
# INIT
x = y = None
# CALC
while True:
answer = calculator(x, y)
user_continue = input(
f"Type 'y' to continue calculating with {answer}, "
f"type 'n' to start a new calculation, or type 'e' to exit the program: ")
if user_continue == "y":
x = answer
elif user_continue == "n":
x = None
else:
if user_continue == "e":
print("You exit the program!")
else:
print("You didn't type 'y', 'n', or 'e'! Program exits automatically!")
# EXIT POINT
break
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment