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 trialJordan Bester
4,752 PointsI 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
231,236 PointsYour 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))
Joseph Guerra
20,674 PointsYou 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.
Troy Huffman
2,799 PointsSteven 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
7,569 PointsThanks guys. Both methods work. Appreciate your time.
A X
12,842 PointsI 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).
gregory fenwick
7,569 Pointsgregory fenwick
7,569 PointsHi,
I tried the same thing and would get infinite loops?
start = 99
while start:
This was copied straight out of the tutorial. Just trying to understand?
Thanks.