我應該構建一個程式,它會自動列印一首歌曲的歌詞(圣誕節的十二天),以便它在每一行中重新列印相同的資訊,但通過與該行相關的新歌詞進行擴展。
例如:
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'''
我被回圈和'“TypeError:'<'在'str'和'int'的實體之間不支持'”卡住了。我所知道的是我將不得不使用 .join() 陳述句。先感謝您。
uj5u.com熱心網友回復:
這是一個幼稚的實作(內容參考):
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()
輸出:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531131.html
上一篇:對熊貓中的列進行子串化
