Question

(Python) 3. 99 Bottles of Beer on the Wall song, recursively Write a function Recurse that...

(Python)

3. 99 Bottles of Beer on the Wall song, recursively
Write a function Recurse that prints out the verses of the famous song, Lyrics of the song 99 Bottles of Beer (shown below). You must called this function recursively.

99 bottles of beer on the wall, 99 bottles of beer.

Take one down and pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.

Take one down and pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall, 97 bottles of beer.

Take one down and pass it around, 96 bottles of beer on the wall.

.

2 bottles of beer on the wall, 2 bottles of beer.

Take one down and pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.

Take one down and pass it around, no more bottles of beer on the wall.


No more bottles of beer on the wall, no more bottles of beer.

Go to the store and buy some more, 99 bottles of beer on the wall.

Type or paste question here

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

# Recurse function which will be called recursively to print the verse

# takes a number as input

# returns when there are no verse to print

def Recurse(num):

    # termination case num=0

    if num==0:

        print("No more bottles of beer on the wall, no more bottles of beer.")

        print("Go to the store and buy some more, 99 bottles of beer on the wall.")

        return

    elif num==1:

        # string formattting using format function that takes arugments

        # equal to the number of braces present in string

        print("{} bottles of beer on the wall,{} bottles of beer.".format(num,num))

        print("Take one down and pass it around,no more bottles of beer.")

        # call Recurse with one number less

        Recurse(num-1)

    else:

        print("{} bottles of beer on the wall,{} bottles of beer.".format(num,num))

        print("Take one down and pass it around,{} bottles of beer.".format(num-1))

        # call Recurse with one number less

        Recurse(num-1)

# main function

def main():

    # initialize num with 99

    num=99

    # call to Recurse()

    Recurse(num)

if __name__ == "__main__":

    main()

Refer to below screenshots for indentation and output

Output

Add a comment
Know the answer?
Add Answer to:
(Python) 3. 99 Bottles of Beer on the Wall song, recursively Write a function Recurse that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a for loop that produces the song Bottles of Beer on the Wall: 10 bottles...

    Write a for loop that produces the song Bottles of Beer on the Wall: 10 bottles of beer on the wall, 10 bottles of beer Take one down, pass it around, 9 bottles of beer on the wall 9 bottles of beer on the wall, 9 bottles of beer Take one down, pass it around, 8 bottles of beer on the wall ... (output continues in the same pattern) ... 1 bottles of beer on the wall, 1 bottles of...

  • Java hw Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the...

    Java hw Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the Wall.” Your program should print the number of bottles in English, not as a number. For example: Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer, Take one down, pass it around, Ninety-eight bottles of beer on the wall. ... One bottle of beer on the wall, One bottle of beer, Take one down, pass it around, Zero bottles of beer on...

  • Problem 1 Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the...

    Problem 1 Write a program that outputs the lyrics for “Ninety-nine Bottles of Beer on the Wall.” Your program should print the number of bottles in English, not as a number. For example: Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer, Take one down, pass it around, Ninety-eight bottles of beer on the wall. ... One bottle of beer on the wall, One bottle of beer, Take one down, pass it around, Zero bottles of beer on...

  • C++ Help!    For this lab, we are going to practice a few different aspects of...

    C++ Help!    For this lab, we are going to practice a few different aspects of programming in C++. ·        Practice using cin and cout. ·        Practice using a loop (for or while) ·        Practice using a c-string. For this lab, you are going to create the code that writes the lyrics for the old song, “99 Bottles of Beer” with a slight change. If you have never seen the song, you can view the lyrics here: http://www.99-bottles-of-beer.net/lyrics.html Our version...

  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • C SHARP PROGRAM LANGUAGE The program below isn't working quite right. First compile and run it...

    C SHARP PROGRAM LANGUAGE The program below isn't working quite right. First compile and run it to familiarize yourself with the output. Then update the program to implement the following requirements: 1. the program shall sing ( display with WriteLine() ) the song's verses for drinks 100 through 91. 2. the program shall not sing the song's verses for drinks 90 - 7. 3. the program shall implement a string variable which is initialized by the programmer to the type...

  • Python Write a function index_last(elem, seq) that takes as inputs an element elem and a sequence...

    Python Write a function index_last(elem, seq) that takes as inputs an element elem and a sequence seq, and that uses recursion (i.e., that calls itself recursively) to find and return the index of the last occurrence of elem in seq. The sequence seq can be either a list or a string. If seq is a string, elem will be a single-character string; if seq is a list, elem can be any value. Don’t forget that the index of the first...

  • Please use Python 3, thank you~ Write a program that: Defines a function called find_item_in_grid, as...

    Please use Python 3, thank you~ Write a program that: Defines a function called find_item_in_grid, as described below. Calls the function find_item_in_grid and prints the result. The find_item_in_grid function should have one parameter, which it should assume is a list of lists (a 2D list). The function should ask the user for a row number and a column number. It should return (not print) the item in the 2D list at the row and column specified by the user. The...

  • PYTHON 1.Guessing many passwords: Write a function called guess_passwords. It doesn't take any arguments. It should use the function guess, trying out as many possible passwords as possible. Here...

    PYTHON 1.Guessing many passwords: Write a function called guess_passwords. It doesn't take any arguments. It should use the function guess, trying out as many possible passwords as possible. Here are some methods you might try: Try out a hardcoded list of strings that you think people might use as passwords. Try out all words in the word file. Try out all character combinations of length 4 or less (or more if you don't mind waiting). Try out combinations of words...

  • Write a Python program that tests the function main and the functions discussed in parts a...

    Write a Python program that tests the function main and the functions discussed in parts a through g. Create the following lists: inStock - 2D list (row size:10, column size:4) alpha - 1D list with 20 elements. beta - 1D list with 20 elements. gamma = [11, 13, 15, 17] delta = [3, 5, 2, 6, 10, 9, 7, 11, 1, 8] a. Write the definition of the function setZero that initializes any one-dimensional list to 0 (alpha and beta)....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT