-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimpleCalculator.py
More file actions
57 lines (34 loc) · 1.45 KB
/
Copy pathsimpleCalculator.py
File metadata and controls
57 lines (34 loc) · 1.45 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# It is a simple python program of a simple calculator which can perform with only 1 operator and n number of operands
# This progam is built without the use of functions and by only using if-else condition and for loop
ls=[]
num=int(input("Enter the number of operands >> "))
ans=0
if num>1:
for i in range(0,num): #storing operands as list
val=str(i+1)
ele=int(input("Enter the element number "+val+" >>"))
ls.append(ele)
operator=str(input("Enter + , - , * , / >>"))
if operator=='+': #addition
for j in ls:
ans=ans+j
print("The answer is >> ",ans)
elif operator=='-': #subtraction
for j in ls:
ans=j-ans
print("The answer is >> ",(-ans))
elif operator=='*': #multiplication
ans=1
for j in ls:
ans=ans*j
print("The answer is >> ",ans)
elif operator=='/' and len(ls)==2: #division
for j in ls:
ans=(ls[0]/ls[1])
print("The answer is >> ",ans)
elif operator=='/' and len(ls)>2:
print("You should use only 2 operands for division ")
else:
print("Please select a valid operator")
else:
print("You should select more than 1 operand") #error