In: Computer Science
(IN PYTHON 3) Complete the following two tasks: 1. Words like first, second, and third are referred to as ordinal numbers. In this exercise, you will write a function that takes an integer as its only parameter and returns a string containing the appropriate English ordinal number as its only result. Your function must handle the integers between 1 and 12 (inclusive). It should return an empty string if a value outside of this range is provided as a parameter. 2. The Twelve Days of Christmas is a repetitive song that describes an increasingly long list of gifts sent to one’s true love on each of 12 days. A single gift is sent on the first day. A new gift is added to the collection on each additional day, and then the complete collection is sent. The first two verses of the song are shown below. The complete lyrics are available online. On the first day of Christmas My true love sent to me: A partridge in a pear tree. On the second day of Christmas My true love sent to me: Two turtle doves, And a partridge in a pear tree. Your task is to write a program that displays the complete lyrics for the 12 Days of Christmas. Write a function that takes the verse number as its parameter and displays the specified verse of the song. Then call that function 12 times with integers that increase from 1 to 12. Each item that is sent to the recipient in the song should only appear once in your program, with the possible exception of the partridge. It may appear twice if that helps you handle the difference between “A partridge in a pear tree” in the first verse and “And a partridge in a pear tree” in the subsequent verses. Import the function from task one to assist.
Part 1: Convert integer to english ordinary number.
Part 2 : Print the Twelve Days of Christmas Song
#-------------PART 1-----------------------------
#Convert integer to ordinary number
def words(number):
#word list contains ordinary numbers
word=["First", "second ", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth", "Eleventh","Twelfth"]
#Check the range
if 1 <= number <= 12:
#Return the ordinary number
return word[number-1]
else:
#return empty string
return ""
#------ PART 2--------------------------------------
#Display Twelve Days of Christmas Song
def verses(number):
#Store verse in a list
verse=['A partridge in a pear tree','Two turtle doves, and','Three French hens ,','Four calling birds,','Five Gold Rings,','Six geese a laying,','Seven swans a swimming,','Eights maids a milking,','Nine ladies dancing,','Ten lords a leaping,','Eleven pipers piping,','Twelve drummers drumming']
#Call method words() to get the ordinary number
print(f"On the {words(number)} day of Christmas my true love gave to me")
#Display verses till that days
print("\n".join(verse[number-1::-1]))
print()
#Call verses 12 times using range()
for i in range(1,13):
verses(i)
Output:
Summary:
Part 1: Function to convert integer number into english ordinary number.
Steps:
Part 2: Print the Twelve Days of Christmas lyrics - takes parameter number(int)
The lyrics starts with On the {first} day of Christmas my true love gave to me . The first varies by the number passed into the method.
Steps:
Call the function verses twelve times for twelve days and pass the day number .
The function uses range(start,end) to generate sequence of number from start to end (end exclusive).