2014-12-09 3 views
3

다음은 크리스마스의 날에 어떤 일이 일어나는지 알 수있게 해줄 것입니다.하지만 어떻게하면 노래처럼 작동하게 될까요? 5 일째는 금반지로 시작하여 거꾸로 작동합니다. 배 나무에?! 목록과 같은 정렬 된 데이터 구조와 관련이 있습니까?Python 크리스마스 12 일

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

감사

+0

색인에서 시작하여 역순으로 반복되는 for 루프를 만듭니다. – marsh

+0

"Xth"문자열을 줄이기위한 기본 제공 방법이 없기 때문에 현재 데이터 구조가 매우 까다로울 수 있습니다. 전의. "일곱 번째"가 주어지면 다음 날이 "여섯 번째"임을 어떻게 알 수 있습니까? – Kevin

+0

왜 열쇠를 서수로 써야합니까? – CoDEmanX

답변

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) 
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) 
+1

두 번째 요소가 아닌 첫 번째 요소로 슬라이스하려면'days_list [index :: - 1]'을 사용해야합니다. –

+0

많은 감사드립니다! 편집 된 솔루션. – Mathmos

1

키를 잘못 사용하셨습니까?

times = [ 'first', 'second', 'third', 'fourth'] 
for t in times: 
    print("On the {} day of Christmas my true love gave to me:".format(days[t])) 
0

그 반복에, 당신의 키와 일치 할 때까지 사용이 OrderedDict이, 뒤쪽으로 반복

days['first']

인쇄를 시작합니다.

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를 사용하여 항목을 역순으로 추가하고 사전을 통해 반복합니다.

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 
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]) 
관련 문제