[英]Python 12 Days of Xmas
The following will allow me to find out what happens on a day of Christmas, but how would I go about getting it to work like the song - so the 5th day would start with gold rings and work backwards to the pear tree?! 以下内容将让我了解圣诞节那天会发生什么,但是我怎样才能让它像歌曲一样开始工作 - 所以第五天会以金戒指开始并向后回到梨树?! Something to do with an ordered data structure like a list?
与列表等有序数据结构有关吗?
day = input('What day of Christmas is it? ')
days = {'first':'A Partridge in a Pear Tree','second':'Two Turtle Doves',
'third':'Three French Hens','fourth':'Four Calling Birds','fifth':'Five Golden Rings',
'sixth':'Six Geese a Laying','seventh':'Seven Swans a Swimming','eighth':'Eight Maids a Milking',
'ninth':'Nine Ladies Dancing','tenth':'Ten Lords a Leaping','eleventh':'Eleven Pipers Piping',
'twelfth':'12 Drummers Drumming'}
print('On the',day.lower(),'day of Christmas my true love gave to me:')
print(days[day.lower()])
Thanks 谢谢
There's no need to use a dictionary when a tuple will do the job. 当元组完成工作时,不需要使用字典。 Instead of using the words as indexes, you can use their position in the list as an index.
您可以将它们在列表中的位置用作索引,而不是将单词用作索引。
>>> days = (('first', 'A Partridge in a Pear Tree'),
('second', 'Two Turtle Doves'),
('third', 'Three French Hens'),
('fourth', 'Four Calling Birds'),
('fifth', 'Five Golden Rings'),
('sixth', 'Six Geese a Laying'),
('seventh', 'Seven Swans a Swimming'),
('eighth', 'Eight Maids a Milking'),
('ninth', 'Nine Ladies Dancing'),
('tenth', 'Ten Lords a Leaping'),
('eleventh', 'Eleven Pipers Piping'),
('twelfth', '12 Drummers Drumming'))
>>> daynum = int(input('What day of Christmas is it? '))
... for i in range(daynum - 1, -1, -1):
... if i == daynum - 1:
... print("On the {} day of Christmas my true love gave to me: ".format(days[i][0]))
... if i == 0 and daynum != 1: # if it's the first day and there isn't only 1 day
... print("And ", end='')
... print(days[i][1])
What day of Christmas is it? 4
On the fourth day of Christmas my true love gave to me:
Four Calling Birds
Three French Hens
Two Turtle Doves
And A Partridge in a Pear Tree
You could organise your data in a list and then iterate on it in reverse order: 您可以在列表中组织数据,然后以相反的顺序迭代它:
sentences = [
'A Partridge in a Pear Tree',
'Two Turtle Doves',
'Three French Hens',
'...',
]
days = [ 'first', 'second', 'third', 'fourth', '...' ]
day = input('What day of Christmas is it? ')
print("On the {} day of Christmas my true love gave to me:".format(days[day]))
for sentence in list(reversed(sentences))[-day:]:
print(sentence)
Sorted it with a dictionary and a list! 用字典和列表对它进行排序!
day = int(input('What day of Christmas is it? (1-12) '))
days_dict = {1:'first',2:'second',3:'third',4:'fourth',5:'fifth',6:'sixth',7:'seventh',8:'eighth',
9:'ninth',10:'tenth',11:'eleventh',12:'twelfth'}
days_list = ['And a Partridge in a Pear Tree!','Two Turtle Doves','Three French Hens','Four Calling Birds',
'Five Golden Rings','Six Geese a Laying','Seven Swans a Swimming','Eight Maids a Milking',
'Nine Ladies Dancing','Ten Lords a Leaping','Eleven Pipers Piping','12 Drummers Drumming']
print('On the',days_dict[day],'day of Christmas my true love gave to me:')
result = days_list[day-1::-1]
if day == 1:
print('A partridge in a pair tree')
else:
for item in result:
print(item)
What's wrong using the key? 使用钥匙有什么问题?
days['first']
times = [ 'first', 'second', 'third', 'fourth']
for t in times:
print("On the {} day of Christmas my true love gave to me:".format(days[t]))
Use an OrderedDict, iterate backwards until you match your key, on that iteration start printing. 使用OrderedDict,向后迭代,直到匹配您的键,在该迭代开始打印。
day = input('What day of Christmas is it? ')
days = OrderedDict([
('first', 'A Partridge in a Pear Tree'),
('second', 'Two Turtle Doves'),
('third', 'Three French Hens'),
('fourth', 'Four Calling Birds'),
('fifth', 'Five Golden Rings'),
('sixth', 'Six Geese a Laying'),
('seventh', 'Seven Swans a Swimming'),
('eighth', 'Eight Maids a Milking'),
('ninth', 'Nine Ladies Dancing'),
('tenth':'Ten Lords a Leaping'),
('eleventh', 'Eleven Pipers Piping'),
('twelfth', '12 Drummers Drumming')
])
found = False
print('On the {} day of Christmas, my true love gave to me:'.format(day.lower()))
for ordinal_day, gift in reversed(days.items()):
if ordinal_day == day:
found = True
if found:
print('\t'+gift)
if not found:
print ('\tBupkis, input must be "first", "second", etc.')
Alternatively, use an OrderedDict, add the items in reverse order, and iterate forwards through the dictionary. 或者,使用OrderedDict,以相反的顺序添加项目,并在字典中向前迭代。
Just use a list. 只需使用一个列表。 Ask the day and reverse print the list.
询问当天并反向打印列表。
days_of_christmas = (('first', 'A Partridge in a Pear Tree.'), ('second', 'Two Turtle Doves, and'),
('third', 'Three French Hens,'), ('fourth', 'Four Calling Birds,'),
('fifth', 'Five Golden Rings,'), ('sixth', 'Six Geese a Laying,'),
('seventh', 'Seven Swans a Swimming,'), ('eighth', 'Eight Maids a Milking,'),
('ninth', 'Nine Ladies Dancing,'), ('tenth', 'Ten Lords a Leaping,'),
('eleventh', 'Eleven Pipers Piping,'), ('twelfth', 'Twelve Drummers Drumming,'))
day = int(input('What day of Christmas is it? (1-12) ')) - 1
print("\nOn the {} day of Christmas my true love sent to me: ".format(days_of_christmas[day][0]))
for gift in range(day, -1, -1):
print(days_of_christmas[gift][1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.