-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (49 loc) · 1.69 KB
/
main.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
import art
# Define functions to perform calculations
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1 - n2
def multiply(n1, n2):
return n1 * n2
def divide(n1, n2):
return n1 / n2
# Initialise a dictionary of mathematical operators as keys and function names as values
operations = {}
keys = ["+", "-", "*", "/"]
values = [add, subtract, multiply, divide]
for key in keys:
operations[key] = values[keys.index(key)]
# Call a function whose name corresponds to the key in operations dict
# and pass the user input as arguments to the function
def calculate(operation, n1, n2):
if operation == "+":
return operations["+"](n1, n2)
elif operation == "-":
return operations["-"](n1, n2)
elif operation == "*":
return operations["*"](n1, n2)
elif operation == "/":
return operations["/"](n1, n2)
def calculator():
print(art.logo)
n1 = int(input("Enter number 1: "))
operation = input("Choose operation (type +, -, * or /): ")
n2 = int(input("Enter number 2: "))
result = calculate(operation, n1, n2)
print(f"{n1} {operation} {n2} = {result}")
# Loop to perform additional calculations
while True:
next_step = input("Type 'y' to perform another calculation, 'n' to start over: ").lower()
if next_step == "y":
n1 = result
operation = input("Choose operation (type +, -, * or /): ")
n2 = int(input("Enter number 2: "))
result = calculate(operation, n1, n2)
print(f"{n1} {operation} {n2} = {result}")
else:
print("\n" * 20)
calculator()
break
if __name__ == "__main__":
calculator()