-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.py
102 lines (95 loc) · 2.96 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#Console Calculator...
print("----------------------- CALCULATOR ------------------------\n..Print 'done' after finishing inputing in both...")
numc =0
funco =0
numlist=list()
function=list()
while True:
while True:
num=input("Enter your Number:- ")
if num == "done":break
try:
number = float(num)
except:
number = 0
if number == 0:
print("..enter a valid number..")
else:
while True:
fun = input("Enter operator(+,-,/,*):- | ")
if fun == "done":break
func = str(fun)
if func in ('+','-','/','*'):
function.append(func)
break
else:
print("..enter a valid operator..")
del func
continue
numlist.append(number)
print("..............................\n")
#print(numlist)
#print(function)
numdict=dict()
funcdict=dict()
# This function adds two numbers
def add(x, y):
return x + y
# This function subtracts two numbers
def subtract(x, y):
return x - y
# This function multiplies two numbers
def multiply(x, y):
return x * y
# This function divides two numbers
def divide(x, y):
return x / y
n=0
f=0
for nu in range(len(numlist)):
num = "num"+str(n)
nums =numlist[nu]
numdict[num] = nums
n = n+1
print(numdict)
for fu in range(len(function)):
fug = "fun"+str(f)
fugn = function[fu]
funcdict[fug] = fugn
f =f + 1
print(funcdict)
v=1
w=0
total= numdict["num0"]
print("\nCounting... step by step..")
for _ in range(len(funcdict)):
if w >= len(funcdict):
break
else:
if funcdict["fun"+str(w)] == '+':
tot = add(total, numdict["num"+str(v)])
print(total,"+", numdict["num"+str(v)],"=",tot)
elif funcdict["fun"+str(w)] == '-':
tot = subtract(total, numdict["num"+str(v)])
print(total,"-", numdict["num"+str(v)],"=",tot)
elif funcdict["fun"+str(w)] == '*':
tot = multiply(total, numdict["num"+str(v)])
print(total,"*", numdict["num"+str(v)],"=",tot)
elif funcdict["fun"+str(w)] == '/':
tot = divide(total, numdict["num"+str(v)])
print(total,"/", numdict["num"+str(v)],"=",tot)
else:
print("Can't do this")
total = tot
#print(total)
v = v +1
w =w+1
print("..............................\nYour Answer :",total)
ask = input("\nDo you want to continue(y/n):")
yes = "y"
if ask == yes:
numlist.clear()
function.clear()
continue
else:
break