-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator using dictionaries & func.py
More file actions
35 lines (30 loc) · 1.01 KB
/
Calculator using dictionaries & func.py
File metadata and controls
35 lines (30 loc) · 1.01 KB
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
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,}
# print(operations["*"](4,8))
def calculator():
stop_calc=False
first_num = float(input("Enter the first number\n"))
while not stop_calc:
for symbol in operations:
print(symbol)
operator_chosen=input('Choose operation\n')
second_num=float(input("Enter the second number\n"))
for i in operations:
if operator_chosen==i:
current_operation_result=operations[i](first_num,second_num)
print(f"{first_num}{operator_chosen}{second_num}={current_operation_result}")
you_continue=input("Type 'y' for yes and 'n' for no\n").lower()
if you_continue=='y':
first_num=current_operation_result
else:
stop_calc=True
print("\n"*30)
calculator() #RECURSION
calculator()