Python Forum

Full Version: Headfirst Python Exercise
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys, I have a question. I'm currently working my way through the Headfirst Python book. And there is an exercise where you create an app based around the song 99 bottles of beer. I'll paste the code here in a moment.
word = "bottles"

for beer_num in range(99, 0, -1):


    print(beer_num, word, "of beer on the wall.")

    print(beer_num, word, "of beer.")

    print("Take one down.")

    print("Pass it around.")

    if beer_num ==1:

        print("No more bottles of beer on the wall.")

    else:

        new_num = beer_num - 1

        if new_num == 1:

            word = "bottle"

        print(new_num, word, "of beer on the wall.")


    print()
My question is regarding beer_num, is this what you write when you want Python to determine how many times a word appears?

I just deleted beer and replaced it with a different word, and it works in the same way

So I guess that it is
In this example, the for loop and range(99, 0, -1) tells the script how many times to loop through something whether it be a word, function, object, etc. beer_num could be an underscore, letter, or word. There are special characters than can not be used.
You can get the same effects with a while loop.

Both give same result
print('----- for loop -----')
for _ in range(10, 0, -1):
    print(_)
bottles = 10
print()
print('----- while loop -----')
while bottles > 0:
    print(bottles)
    bottles -= 1
Output:
----- for loop ----- 10 9 8 7 6 5 4 3 2 1 ----- while loop ----- 10 9 8 7 6 5 4 3 2 1
It comes down to the format of a for loop. Python's for loop is very flexible. In it's simplest form it follows
for item in iterable:
After the for keyword you have a variable. This variable, whatever it is called (beer_num,, item, foo, etc) receives an item from the iterable until the iterable runs out.
The range keyword gives you an iterable, in this case the set of integers from 99 to 1 going down by 1. Note that range stops short of the second parameter. The first parameter is where the range starts, second one short of where it ends, and the third is the step between items in the range.
Examples:
for bottle in range(6): #using just the end parameter in range, defaults 0 and step 1
    print(f'This is {bottle}')
Output:
This is 0 This is 1 This is 2 This is 3 This is 4 This is 5
for loops can also iterate over a list
for state in ['NY','PA','TN','Confusion']:
    print(f'This is {state}')
Output:
This is NY This is PA This is TN This is Confusion
And, you do not have to use the item in the for statement
for _ in range(3):
    print(f'This is Boring Oregon')
Output:
This is Boring Oregon This is Boring Oregon This is Boring Oregon
Keep reading. It helps if you know another programming language, but not necessary. I have several of the Head First books - I like their sense of humor. That book takes you from basic console programs up to using Flask to create web based applications but the curve can be steep.
Thanks guys, I really appreciate it.