Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
l=[]
sum,i=0,0
for x in range(101):
if x%10==0:
l.append(x)
print(l)
while i<101:
sum=sum+l[i]
i=i+1
print("Sum is:",sum)

What I have tried:

l=[]
sum,i=0,0
for x in range(101):
if x%10==0:
l.append(x)
print(l)
while i<101:
sum=sum+l[i]
i=i+1
print("Sum is:",sum)
Posted
Updated 14-Nov-18 21:51pm
Comments
Bryian Tan 15-Nov-18 1:02am    
so, what the issue?

I'm guessing the sum doesn't work? the code should sum what in the list and not loop 101 times because it will cause out of range error.

Python
l=[]
sum,i=0,0
for x in range(101):
	if x % 10 == 0:
    		l.append(x)
		print(l)
            
for i in xrange(1,len(l)):
	sum=sum+l[i]
    
print("Sum is:",sum)


Output
[0]
[0, 10]
[0, 10, 20]
[0, 10, 20, 30]
[0, 10, 20, 30, 40]
[0, 10, 20, 30, 40, 50]
[0, 10, 20, 30, 40, 50, 60]
[0, 10, 20, 30, 40, 50, 60, 70]
[0, 10, 20, 30, 40, 50, 60, 70, 80]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
('Sum is:', 550)
 
Share this answer
 
Python
l=[]
for x in range(10,101,10):
  l.append(x)
  print(l)

print("Sum is:", sum(l))



Or, if you need just the sum:
Python
limit = 101
n = (limit - limit % 10) / 10
sum = n * (n + 1) * 5
print("Sum: ", sum)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900