Python 12 days of Christmas
The following will let me know what's going on on Christmas Day, but how can I make it work like a song - so Day 5 starts with gold rings and goes back 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()])
thank
+3
source to share
6 answers
There is no need to use a dictionary if the tuple does the job. Instead of using words as indices, you can use your 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 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
+3
source to share
You can reorder your data in a list and then repeat 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)
+1
source to share
Sorted with dictionary and 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)
+1
source to share
Use OrderedDict, iterate backward until you pick up your key, then start printing with iteration.
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 OrderedDict, add items in reverse order and iterate forward through the dictionary.
0
source to share
Just use a list. Set the day and 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])
0
source to share