Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python Basics/Logic in Python/Around and Around/ While Loop script Error?

Hi I was working through the Python Basic course. I really like it by the way! Nice work. I have found an error I think? in the code for the while loop in Python Basics/Logic in Python, Around and Around ... When I ran it the code I am getting an Index error : tuple out of range. line 3

I got it to run by creating a variable for the string substitution line and then pass that to print.

bottle_song = "{} bottles of milk on the wall, {} bottles of milk."
start = 99
while start:
    print("bottle_song".format(start))
    print("Take one down and pour it on some cereal.")
    start -= 1
    print("{} bottles of milk on the wall.".format(start))

I do have a question why do I have to put “” on the variable bottle_song? e.g.

print("bottle_song".format(start))

I would have thought that “”"print(bottle_song.format(start))””” would work?

Hi David, I have added the correct formatting to your quesion.

5 Answers

Ah, I see. The reason why this code does not work is because there are 2 places where something should be formatted in but there is only 1 thing being formatted in:

start = 99
while start:
    print("{0} bottles of milk on the wall, {0} bottles of milk.".format(start)) 
    print("Take one down and pour it on some cereal.")
    start -= 1
    print("{} bottles of milk on the wall.".format(start))

On the third line I have added two 0s inside the curly braces to show the first format item should be formatted here. As well as my solution, you could have written start twice in .format() but try not to repeat your code when there is an easier solution.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

The code posted in the OP runs as Haider as said. I am guessing the error came from a version of the code where bottle_song was used as a variable not as a string 'bottle_song'

The "tuple out of range" would be caused by the str.format() method in the print statement.

When formatting the string, the number of fields ({}) must match the number of items in the format() arguments.

The code For the statement:

bottle_song = "{} bottles of milk on the wall, {} bottles of milk."
start = 99
while start:
    print(bottle_song.format(start))
    print("Take one down and pour it on some cereal.")
    start -= 1
    print("{} bottles of milk on the wall.".format(start))

Would produce the "tuple out of range" because there are two fields to fill but only one argument is provided to format()

This can be fixed in two ways: double the arguments, or reuse the argument for both fields:

#  reuse single argument for both fields:
bottle_song = "{0} bottles of milk on the wall, {0} bottles of milk."

# double the arguments:
    print(bottle_song.format(start, start))

Note: Having too few fields to fill in a string is ok:

# legal code
"Some string with no fields".format(1, 2, 3)
# produces
"Some string with no fields"

You might also want to move the "bottle_song" print outside of the while loop.

EDIT: changed answer to update on difference between too many and to few fields when using format on string.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Corrected usage when too few and too many fields are present for string format method

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Hi, Haider, Not as many as I would like since you snagged this one :grin:. I've been on a quest to break 1000 best answers. At my last check, I have 1,171 answers, 960 best answers. The only way I know to get this number is to see your own name come up as an "Ask to answer" choice on unanswered questions. I highest I've come across for anyone so far is 1062 best answers (my target: 1063 :grin:). Admission: Points systems cause obsessive behavior.

Wow! Haha, guess I've got a long way to go then.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Just saw: Haider Ali • 11603 • 46 answers • 31 best

Hi, here is a link to the lesson: <a href="https://teamtreehouse.com/library/python-basics/logic-in-python/around-and-around" target="_blank">around-and-around</a>.

What I posted was my fix for the while loop example code.

Here is what is in the lesson:

start = 99
while start:
    print("{} bottles of milk on the wall, {} bottles of milk.".format(start))
    print("Take one down and pour it on some cereal.")
    start -= 1
    print("{} bottles of milk on the wall.".format(start))

Also, on your formatting, there has to be a space between the 3 backticks and the line before :)

Ah yes! That make sense. Thank you!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

On formatting, when using the three backticks (```), be sure to leave a blank line above and below the block for full effect. Adding a code type like ```python will add the appropriate syntax coloring. Inside of a triple-backtick code block, single backticks (`) should not be used as it messes with the format.