Reputation: 145
I'm trying to write a very simple program, I want to print out the sum of all the multiples of 3 and 5 below 100, but, an error keeps accuring, saying "invalid literal for int() with base 10:" my program is as follows:
sum = ""
sum_int = int(sum)
for i in range(1, 101):
if i % 5 == 0:
sum += i
elif i % 3 == 0:
sum += i
else:
sum += ""
print sum
Any help would be much appreciated.
Upvotes: 9
Views: 30265
Reputation: 454970
The ""
are the cause of these problems.
Change
sum = ""
to
sum = 0
and get rid of
else:
sum += ""
Upvotes: 10
Reputation: 145
Ok, I'm new to Python so I was doing quite a few silly things; anyway, I think I've worked it out now.
sum = 0
for i in range(1, 1001):
if i % 5 == 0:
sum += i
elif i % 3 == 0:
sum += i
print sum
Upvotes: 3
Reputation: 40700
Python is not JavaScript: ""
does not automatically convert to 0
, and 0
does not automatically convert to "0"
.
Your program also seems to be confused between printing the sum of all the multiples of three and five and printing a list of all the numbers which are multiples of three and five.
Upvotes: 7