question: Create a program that has a list of month names (January, February, etc.) and takes another list of day numbers (1 – 31) and assigns all the correct numbers with the month in another list (January 1-31, February 1-28, March 1-31, etc.). Print out all the elements in the list. Then Shuffle the list and print one element in the list. Make sure to tell the user this is your lucky day of the year!
ex.
months = [January, February, etc.]
days = [1,2,3,4,5,6,7,8,9…31]
empty = [January 1, January 2, January 3… January 31, February 1, February 2…February 28…]
this is what I have so far:
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
combined = []
for m in months:
for d in days:
combined.append(days[0:30] + months[0])
combined.append(days[0:27] + months[1])
combined.append(days[0:30] + months[2])
combined.append(days[0:29] + months[3])
combined.append(days[0:30] + months[4])
combined.append(days[0:29] + months[5])
combined.append(days[0:30] + months[6])
combined.append(days[0:30] + months[7])
combined.append(days[0:29] + months[8])
combined.append(days[0:30] + months[9])
combined.append(days[0:29] + months[10])
combined.append(days[0:30] + months[11])
shuffle(combined)
final = combined
print (final)
I’m getting an error that says "can only concatenate list (not "str") to list.
Does anyone have a fix, it would be greatly appreciated
>Solution :
can only concatenate list (not "str") to list
days[0:30] returns an list of values, and months[0] returns a string. That is the error being reported.
An easier way to construct the data would be to use a dictionary lookup on months vs the number of days, and then use a range to get each individual day.
month_counts = {
"January": 31,
"February": 28,
"March": 31,
"April": 30,
"May": 31,
"June": 30,
"July": 31,
"August": 31,
"September": 30,
"October": 31,
"November": 30,
"December": 31,
}
combined = []:
for month, count in month_counts.items():
for day in range(1, count + 1):
combined.append(f"{month} {day}")