-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 10 project calculator.py
60 lines (36 loc) · 1.08 KB
/
Day 10 project calculator.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
def add(n1, n2):
return n1 + n2
def subtract(n1, n2):
return n1 - n2
def multiplication(n1, n2):
return n1 * n2
def division(n1, n2):
return n1 / n2
operations = {
"+" : add,
"-" : subtract,
"*" : multiplication,
"/" : division
}
def calculator():
num1 = float(input("Type the first number: "))
for symbol in operations:
print(symbol)
should_continue = True
while should_continue:
symbol = input("Type operation symbol you want to use: ")
next_num = float(input("Type the next number: "))
calculation_function = operations[symbol]
answer = calculation_function(num1, next_num)
print(f"{num1} {symbol} {next_num} = {answer}")
if input(f"Type 'y' to continue calculating with {answer}: or type 'n' to start a new calculation: ") == "y":
num1 = answer
else:
calculator()
calculator()
# Update 17.12.23: We may use 4 arithmetic functions as inputs of one higher-order function.
# def calculator(n1, n2, func):
# return func(n1, n2)
# example
# result = calculator (2, 3, multiply)
# result is 6