Adding up the numbers

Since adding up the numbers is the purpose of the program, we will dive into that section first. One of the benefits of literate programming is that you can go in any order you please. Therefore, you can explain the most important concepts first, even if they appear later in the source file.

The code in this section will generate a range from 1 to number + 1, and then iterate through and add them all to the variable total.


Add up numbers from one to the number given =

for num in range(1, number + 1):
        total = total + num

We now have a variable total that has not been initialized. Although we could do it here because Python allows that, we will put all variable initializations in the "Initialize variables" section to show some additional features of Literate Programming. The total variable needs to be initialized to 0.

Initialize variables =

total = 0