File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments