Skip to content

Commit d42f26c

Browse files
authored
Add files via upload
1 parent 29fabf1 commit d42f26c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

CalculatorProject/main.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import os
2+
3+
def add(a,b):
4+
return a+b
5+
def sub(a,b):
6+
return a-b
7+
def mul(a,b):
8+
return a*b
9+
def div(a,b):
10+
if b==0:
11+
return "invalid number."
12+
else:
13+
return a/b
14+
15+
16+
operation={
17+
"+":add,
18+
"-":sub,
19+
"*":mul,
20+
"/":div
21+
}
22+
23+
def calculator():
24+
num1 = float(input("Enter the number one: "))
25+
for symbol in operation:
26+
print(symbol)
27+
flag = True
28+
while flag:
29+
op_symbol = input("Pick an operator: ")
30+
num2 = float(input("Enter the number two: "))
31+
calculator_function = operation[op_symbol]
32+
output = calculator_function(num1, num2)
33+
print(f"{num1} {op_symbol} {num2} = {output}")
34+
35+
should_cont = input(
36+
f"Enter 'y' to continue calculation with {output} or 'n' to start with new calculation or 'x' to exit: ").lower()
37+
if should_cont == 'y':
38+
num1 = output
39+
elif should_cont == 'n':
40+
flag = False
41+
os.system('cls')
42+
calculator()
43+
else:
44+
flag = False
45+
print("Bye.")
46+
47+
48+
calculator()
49+
50+
51+
52+
53+

0 commit comments

Comments
 (0)