-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
40 lines (32 loc) · 955 Bytes
/
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
# calculator
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
operations ={
"+": add,
"-": subtract,
"*": multiply,
"/": divide,
}
def calculator():
num1 = float(input("type 1st num: "))
should_continue = True
while should_continue:
for symbol in operations:
print(symbol)
operation_symbol = input("Pick and operation from above: ")
num2 = float(input("type next num: "))
calculation_function = operations[operation_symbol]
answer = calculation_function(num1, num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
if input(f"Type 'y' to continue calculating with {answer} , or type 'n' to start a new calculation: ") == 'y':
num1 = answer
else:
should_continue = False
calculator()
calculator()