Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Headfirst Python Exercise
#1
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
Yoriz write Jun-05-2022, 10:50 AM:
Please post all code, output and errors (In their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
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.
Reply
#4
Thanks guys, I really appreciate it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrong code in Python exercise MaartenRo 2 1,542 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  Python Exercise Doubt azure 4 2,686 Apr-21-2020, 01:15 PM
Last Post: azure
  New to python, having trouble with an exercise Salkay 3 2,169 Feb-18-2020, 01:42 AM
Last Post: Salkay
  Exercise 20, Learn Python 3 the Hard Way Dixon 6 6,073 Feb-26-2018, 04:54 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020