WSQ04 – ‘For’ Loops (Python3)

--Originally published at Elu's Blog

For this assignment these were my instructions:

Write a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10

This is what I came up with:

Captura de pantalla 2017-02-14 a la(s) 09.39.47.png

Captura de pantalla 2017-02-14 a la(s) 09.40.59.png

As we can see in the program above, first, we ask for two integers and then we assign the zero value to the “result” variable. After that we use a ‘for’ loop  for adding every number in the range that we assigned.

To use a for loop, we need to call it by writing ‘for’, then a new variable that will represent each thing in the range that we give and then we write the range. the range in a for loop can be strings, lists or traditional ranges.

Alternative Explanation

Python 3 Programming Tutorial – For loop by sentdex