Python 12天的圣诞节

时间:2014-12-09 18:33:10

标签: python

以下内容将让我了解圣诞节当天会发生什么,但是我怎样才能让它像歌曲一样开始工作 - 所以第五天会以金戒指开始并向后退到梨树上? !与列表等有序数据结构有关吗?

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()])

由于

6 个答案:

答案 0 :(得分:3)

当元组完成工作时,不需要使用字典。您可以使用它们在列表中的位置作为索引,而不是将单词用作索引。

>>> 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

答案 1 :(得分:1)

您可以在列表中组织数据,然后以相反的顺序迭代它:

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)

答案 2 :(得分:1)

用字典和列表对其进行排序!

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)

答案 3 :(得分:1)

使用密钥有什么问题?

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]))

答案 4 :(得分:0)

使用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.')

或者,使用OrderedDict,以相反的顺序添加项目,并在字典中向前迭代。

答案 5 :(得分:0)

只需使用一个列表。询问当天并反向打印列表。

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])