Home > OS >  How to use two lists, one with months, one with days, and use a third list that assigns all the corr
How to use two lists, one with months, one with days, and use a third list that assigns all the corr

Time:04-11

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

CodePudding user response:

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}")

CodePudding user response:

The accepted solution is great and was coincidentally posted just before I was going to post mine. However, I believe mine achieves a little more generality and attempts to answer other parts of OP's question.

Since it is possible that the year in question is a leap year, it could be useful to also test for this. I have also added code to randomly select a day from the year. I was not sure if you actually wanted to reorder the list with shuffle or if you just wanted to select a random element from the list.

from random import randrange

# key: value <-> month: days in months
months = {
    "January": 31,
    "February": 28,
    "March": 31,
    "April": 30,
    "May": 31,
    "June": 30,
    "July": 31,
    "August": 31,
    "September": 30,
    "October": 31,
    "November": 30,
    "December": 31
    }

# year
year = 1996

# check if leap year (1996 is) 
if (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)):
    months["February"] = 29
    days_in_year = 366
else:
    months["February"] = 28
    days_in_year = 365

# list with days in year
combined = []
for month in months:
    for i in range(1, months[month]   1):
        combined.append(f"{month} {i}")

# select random element from list
r = combined[randrange(0, len(combined))]
print(f"Your lucky day is {r}")

Example output

Your lucky day is April 29
  • Related