-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10_Calculator_program.py
78 lines (63 loc) · 2.64 KB
/
Day10_Calculator_program.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Import required modules
from replit import clear
logo = """
_____________________
| _________________ |
| | Pythonista 0. | | .----------------. .----------------. .----------------. .----------------.
| |_________________| | | .--------------. || .--------------. || .--------------. || .--------------. |
| ___ ___ ___ ___ | | | ______ | || | __ | || | _____ | || | ______ | |
| | 7 | 8 | 9 | | + | | | | .' ___ | | || | / \ | || | |_ _| | || | .' ___ | | |
| |___|___|___| |___| | | | / .' \_| | || | / /\ \ | || | | | | || | / .' \_| | |
| | 4 | 5 | 6 | | - | | | | | | | || | / ____ \ | || | | | _ | || | | | | |
| |___|___|___| |___| | | | \ `.___.'\ | || | _/ / \ \_ | || | _| |__/ | | || | \ `.___.'\ | |
| | 1 | 2 | 3 | | x | | | | `._____.' | || ||____| |____|| || | |________| | || | `._____.' | |
| |___|___|___| |___| | | | | || | | || | | || | | |
| | . | 0 | = | | / | | | '--------------' || '--------------' || '--------------' || '--------------' |
| |___|___|___| |___| | '----------------' '----------------' '----------------' '----------------'
|_____________________|
"""
# Define arithmetic operations as functions
def add(n1, n2):
"""Add two numbers"""
return n1 + n2
def sub(n1, n2):
"""Subtract two numbers"""
return n1 - n2
def multi(n1, n2):
"""Multiply two numbers"""
return n1 * n2
def div(n1, n2):
"""Divide two numbers"""
if n2 == 0:
raise ValueError("Cannot divide by zero!")
return n1 / n2
# Create a dictionary to map operation symbols to functions
operations = {"+": add, "-": sub, "*": multi, "/": div}
def calculator():
"""
Main calculator function
"""
print(logo)
num1 = float(input("What's the first number?: "))
# Display available operations
print("Available operations:")
for symbol in operations:
print(symbol)
should_continue = True
while should_continue:
# Get user input for operation and second number
operation_symbol = input("Pick an operation from the line above: ")
num2 = float(input("What's the second number?: "))
# Perform calculation
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
# Ask user if they want to continue
response = input(f"Type 'y' to continue calculating with {answer}, or type 'n' to exit.: ")
if response == "y":
num1 = answer
else:
should_continue = False
clear()
# Call the calculator function
calculator()