0

I'm attempting to write a Python program that will print out the lyrics of 99 Bottles of Beer in chunks, which I've gotten to work. However, I need the last line to say "1 bottle of beer" as opposed to "1 bottles of beer".

I wrapped my code in an if statement, but it appears the if statement is getting ignored. What am I doing wrong?

verse = '''
{some} bottles of beer on the wall
{some} bottles of beer
Take one down, pass it around
{less} bottles of beer on the wall
'''

verse2 = '''
{some} bottles of beer on the wall
{some} bottles of beer
Take one down, pass it around
1 bottle of beer on the wall
'''

for bottles in range(99 , 1, -1):
  if bottles >= 2:
    print(verse.format(some = bottles, less = bottles - 1))
  else:
    print(verse2.format(some = bottles))

2 Answers 2

2

Try this:

print(list(range(99 , 1, -1)))

The result should be:

[99, 98, 97, ..., 5, 4, 3, 2]

Number 1 never appeared.

To Fix this issue, simply changing range(99, 1, -1) to range(99, 0, -1).

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh, I can't believe I missed that, that's exactly it. Thank you for being my second pair of eyes!
1

Ranges don't include the endpoint, so the final element of your range is not 1 but 2.

Should you change the if-statement to if bottles > 2: instead, the output will be correct.

1 Comment

Sure enough, that also did the trick. Thank you for the assistance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.