Start New Discussion within our Software Development Community

Sample code credited to Enalicho

In this example, we count the number of appearances of the number 1 through a range of pages using while loops.

First off, notice how easy it is to tell what the variables in this method are doing. I did not write this method, but I can tell what each variable is used for based on its name. Try to emulate this. This will help you--in any language--as you write more complex code.

I trust the first two lines of the method (lines 2 and 3 of the code snippet) are clear enough, so I'll start with the while loop. If not, please don't hesitate to ask.

while current_page <= number_of_pages:

Starting at current_page = 1, run through this loop until current_page is NOT less than or equal to number_of_page, or in other words, until we run out of pages. Notice that number_of_pages is the number we passed to the method.

page_number = str(current_page)

This line can be a bit tricky. str(current_page) converts current_page to a string. We save this string to the variable page_number. The reason we do this is seen in the next line.

total_number_of_ones += page_number.count('1')

First, let's talk about the page_number.count('1') portion of this line. count() is a method that is used for strings which counts how many times another string occurs within the original string. This is perfectly suited for our purpose! We can use this to count how many times the string '1' occurs within the current_page. Notice that this ' ' surrounds the number 1. We are passing the count() a string that contains the digit 1, not an integer 1, because that is what count() is expecting us to give it.
Also, take not of the += in the middle of the above statement. This does something very useful. += is a shorthand way of saying total_number_of_ones = total_number_of_ones + page_number.count('1'). If we used total_number_of_ones = page_number.count('1'), then total_number_of_ones would always be reset to the number of ones in the current page number. Using += is a very easy way to create a variable that keeps track of the sum of something.

current_page  += 1

This line is a "gotcha" for programming beginners. If you forget to increment this, current_page <= number_of_pages from the while loop will never stop -- your while loop will run forever!

return total_number_of_ones

This line tells Python to give, or return, the expression -- here total_number_of_ones -- to whatever called the method. For example, if you used this statement in a different method,

print count_pages(20)

the total_number_of_ones inside count_pages() is given to the print statement to print out. If you don't return total_number_of_ones, print will not be given anything to print and will simply print None.

I hope this write-up helped you understand while loops!

def count_pages(number_of_pages):
    total_number_of_ones = 0
    current_page = 1
    while current_page <= number_of_pages:
         page_number = str(current_page)
         total_number_of_ones += page_number.count('1')
         current_page  += 1
    return total_number_of_ones