-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
46 lines (32 loc) · 1007 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
41
42
43
44
45
46
# Calculator
def addition(n1, n2):
return n1 + n2
def subtraction(n1, n2):
return n1 - n2
def multiplication(n1, n2):
return n1 * n2
def division(n1, n2):
return n1 / n2
operations = {
"+": addition,
"-": subtraction,
"*": multiplication,
"/": division
}
def calculator():
num1 = float(input("What is the first number?: "))
for symbol in operations:
print(symbol)
should_continue = True
while should_continue:
operation_symbol = input("Select your function: ")
num2 = float(input("What is the next number?: "))
calculation_function = operations[operation_symbol]
answer = calculation_function(n1=num1, n2=num2)
print(f"{num1} {operation_symbol} {num2} = {answer}")
if (input(f"Type 'Y' to continue calculation with {answer} or type 'N' to start over? Y/N ")).lower() == "y":
num1 = answer
else:
should_continue = False
calculator()
calculator()