0
Python: How to add numbers printed from a for loop from a specific range?
I already have the for loop that prints odd numbers from a specific range. But now I have to add all of these....and I just can't figure it out... :( Thanks for your advice!!
3 Answers
+ 3
variable = 0
for loop start:
conditions if any:
variable += iterator
print(variable)
+ 2
You can use the sum() function and a comprehension
print(sum(i for i in range(3, 20) if i % 2))
+ 1
You set a variable to zero before the loop.
And then, with every number of the range, you add that number to it.