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 (2015) Logic in Python Around and Around

Jordan Bester
Jordan Bester
4,752 Points

I am trying to do the bottles of beer on a wall loop but it will not work

start = 99
while start:
... print ("{} bottles of beer on the wall, {} bottles of beer." .format(start))
... print ("Take one down and pour it on some cereal.")
... start -= 1
... print ("{} bottles of beer on the wall." .format(start))
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IndexError: tuple index out of range

5 Answers

Steven Parker
Steven Parker
231,271 Points

:point_right: Your first print template has two placeholders, but you only pass the format one value.

Pass your value twice, once for each placeholder:

     print ("{} bottles of beer on the wall, {} bottles of beer.".format(start,start))
gregory fenwick
gregory fenwick
7,569 Points

Hi,

I tried the same thing and would get infinite loops?

start = 99

while start:

print("{} bottles of milk on the wall, {} bottles of milk.".format(start,start))

print("Take one down and pour it on some cereal.")

start -= 1

print("{} bottles of milk on the wall.".format(start))

This was copied straight out of the tutorial. Just trying to understand?

Thanks.

Joseph Guerra
Joseph Guerra
20,674 Points

You should add a condition to your while clause, to end the loop. You don't want an infinite loop!

start = 99
while start > 0:
  print("{} bottles of beer on the wall, {} bottles of beer. Take one down, pass it around.".format(start, start))
  start -= 1

Now the while loop runs while start is greater than 0. Then it stops.

Steven is right about passing two 'start' variables to the two place holders. I like the fact that you can use the while loop without a condition statement as it will evaluate a variable as 'true' or 'false'. The problem with the way the example code is written is that start is initialized to '99' and therefore is start is false on the first iteration.

Try running this:

start = 99 condition = True while condition: print("{} bottles of milk on the wall, {} bottles of milk".format(start, start)) print("Take one down, pass it around... ") start -= 1 print("{} bottles of milk on the wall.".format(start)) print("") if start == 0: condition = False

gregory fenwick
gregory fenwick
7,569 Points

Thanks guys. Both methods work. Appreciate your time.

I also wanted to comment, I assume that your title was a typo, but they're called "While loops" (vs. wall loops or the captioning on the video calls them wild loops).