Home > OS >  automatically appended string loop
automatically appended string loop

Time:11-10

I was supposed to built a program that would automatically print the lyrics of a song (twelve days of christmas) so that it re-prints the same message in each line, but extended by the new lyric pertaining to that line.

For instance:

verse1 = '''On the first day of Christmas
my true love sent to me:
A Partridge in a Pear Tree''''

verse2 = '''On the second day of Christmas
my true love sent to me:
2 Turtle Doves
and a Partridge in a Pear Tree'''

I get stuck with the loops and ' "TypeError: '<' not supported between instances of 'str' and 'int'" '. What I do know is that I will have to use the .join() statement. Thank you in advance.

CodePudding user response:

Here is a naive implementation (reference for the content):

verses = ['a partridge in a pear tree', 'two turtle doves', 'three French hens',
          'four calling birds', 'five gold rings', 'six geese a-laying',
          'seven swans a-swimming', 'eight maids a-milking', 'nine ladies dancing',
          'ten lords a-leaping', 'eleven pipers piping', 'twelve drummers drumming']

days = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth',
        'seventh', 'eigth', 'ninth', 'tenth', 'eleventh', 'twelfth']

for i, day in enumerate(days):
    print(f'On the {day} day of Christmas,\nmy true love sent to me:')
    for verse in verses[i::-1]:
        print(verse)
    if i == 0:
        verses[0] = 'and '   verses[0]
    print()

Output:

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

On the third day of Christmas,
my true love sent to me:
three French hens
two turtle doves
and a partridge in a pear tree

On the fourth day of Christmas,
my true love sent to me:
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the fifth day of Christmas,
my true love sent to me:
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the sixth day of Christmas,
my true love sent to me:
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the seventh day of Christmas,
my true love sent to me:
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the eigth day of Christmas,
my true love sent to me:
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the ninth day of Christmas,
my true love sent to me:
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the tenth day of Christmas,
my true love sent to me:
ten lords a-leaping
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the eleventh day of Christmas,
my true love sent to me:
eleven pipers piping
ten lords a-leaping
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the twelfth day of Christmas,
my true love sent to me:
twelve drummers drumming
eleven pipers piping
ten lords a-leaping
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree
  • Related