Ninety Nine Bottles

Tags: tiny, beginner, scrolling

“Ninety-Nine Bottles” is a folk song of undetermined origin known for its length and repetitiveness. The lyrics go, “Ninety-nine bottles of milk on the wall, ninety-nine bottles of milk. Take one down, pass it around, ninety-eight bottles of milk on the wall.” As the lyrics repeat, the number of bottles falls from ninety-eight to ninety-seven, then from ninety-seven to ninety-six, until it reaches zero: “One bottle of milk on the wall, one bottle of milk. Take it down, pass it around, no more bottles of milk on the wall!”

Luckily for us, computers are excellent at performing repetitive tasks, and this program reproduces all of the lyrics programmatically. An extended version of this program is in Project 51, “niNety-nniinE BoOttels.”

ninety_nine_bottles.py
 1"""Ninety-Nine Bottles of Milk on the Wall
 2By Al Sweigart al@inventwithpython.com
 3Print the full lyrics to one of the longest songs ever! Press
 4Ctrl-C to stop.
 5This code is available at https://nostarch.com/big-book-small-python-programming
 6Tags: tiny, beginner, scrolling"""
 7
 8import sys, time
 9
10print('Ninety-Nine Bottles, by Al Sweigart al@inventwithpython.com')
11print()
12print('(Press Ctrl-C to quit.)')
13
14time.sleep(2)
15
16bottles = 99  # This is the starting number of bottles.
17PAUSE = 2  # (!) Try changing this to 0 to see the full song at once.
18
19try:
20    while bottles > 1:  # Keep looping and display the lyrics.
21        print(bottles, 'bottles of milk on the wall,')
22        time.sleep(PAUSE)  # Pause for PAUSE number of seconds.
23        print(bottles, 'bottles of milk,')
24        time.sleep(PAUSE)
25        print('Take one down, pass it around,')
26        time.sleep(PAUSE)
27        bottles = bottles - 1  # Decrease the number of bottles by one.
28        print(bottles, 'bottles of milk on the wall!')
29        time.sleep(PAUSE)
30        print()  # Print a newline.
31
32    # Display the last stanza:
33    print('1 bottle of milk on the wall,')
34    time.sleep(PAUSE)
35    print('1 bottle of milk,')
36    time.sleep(PAUSE)
37    print('Take it down, pass it around,')
38    time.sleep(PAUSE)
39    print('No more bottles of milk on the wall!')
40except KeyboardInterrupt:
41    sys.exit()  # When Ctrl-C is pressed, end the program.

https://inventwithpython.com/bigbookpython/project50.html