Question
Python programming. Part A For Part A, write a function with recursion that sings (i.e. prints) the North American road trip song, 99 Bottles of
Python programming.
Part A
For Part A, write a function with recursion that sings (i.e. prints) the North American road trip song, 99 Bottles of Beer
The function is defined as follows:
# function: sing
# input: a bottle count value (integer)
# processing: prints the lyrics to the song 99 Bottles of beer
# output: does not return anything
Example usage and output:
>>> sing(5)
5 bottles of beer on the wall, 5 bottles of beer.
Take one down, pass it around, 4 bottles of beer on the wall.
4 bottles of beer on the wall, 4 bottles of beer.
Take one down, pass it around, 3 bottles of beer on the wall.
3 bottles of beer on the wall, 3 bottles of beer.
Take one down, pass it around, 2 bottles of beer on the wall.
2 bottles of beer on the wall, 2 bottles of beer.
Take one down, pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Take it down, pass it around, no more bottles of beer on the wall!
>>> sing(test)
Invalid data
Some notes on your function:
One bottle is singular, while Two bottles is plural. This grammar should be correctly output by your program.
The last verse says, no more bottles of beer on the wall! instead of 0 bottles of beer on the wall.
You should not assume that the user will always pass in an integer, so be sure to do data checking
You must have at least one try, except statement in your function
I will test your function using floating point values, so be sure you handle decimal numbers (i.e. 1.8, 10.0, etc), its up to you how you handle them
You are not permitted to use a for or while loop in your function
After youve defined your recursive function, be sure to call it.
Here is my non-recursive program:
while True: #while TRUE bottles = input('How many bottles of beer on the wall? ') #asking user for amount of bottles if bottles == 'end': #if user types 'end' program will stop print("Goodnight, let's play again later.") break bottles = int(bottles) #turn bottles(string) into an integer if bottles <= 0: #if bottles is equal to 0 or less than 0 user needs to type in a valid number print("Sorry, that's not a valid number of bottles. Try again.") else: #if user tpyes in a valid number, program will work print("OK, here we go!") for i in range(bottles, 0, -1): #setting up proper range if i == 1: # if bottle is on '1' then a specific text will be printed. print("1 bottle of beer on the wall, 1 bottle of beer.") print("Take it down, pass it around, no more bottles of beer on the wall!") else: print(str(i) + " bottles of beer on the wall, " + str(i) + " bottles of beer.") if i == 2: # setting up singular bottle b = "bottle" else: b = "bottles" #plural bottles print("Take one down, pass it around, " + str(i-1) + " " + b + " of beer on the wall.") #decrementing the plural bottles down until it reaches singular bottle print() print()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started