Question: You were asked to put the months of the year in the appropriate seasons. Your task is to create a list of months (January, February, March, etc.) and a list of seasons (Spring, Summer, Fall, Winter) and put in the appropriate month with season in an empty list. You will then output the list using a FOR loop create two empty lines, shuffle the list, and create another for loop to navigate the shuffled list in reversed order.
This is what I have so far:
seasons = ["Spring", "Summer", "Fall", "Winter"]
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
combined = []
for s in seasons:
for m in months:
combined.append(months[0:2] , "is in the season " , seasons[3])
combined.append(months[3:5] , "is in the season " , seasons[0])
combined.append(months[6:8] , "is in the season" , seasons[2])
combined.append(months[9:11] , "is in season" , seasons[0])
I am getting an error that says append() takes exactly one argument (3 given) I somewhat understand what this error means but have no clue how to fix it, does anyone have a fix, it would be greatly appreciated
CodePudding user response:
Here's the way to get your 12 entries:
seasons = ["Winter", "Spring", "Summer", "Fall"]
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
combined = []
for i,s in enumerate(seasons):
for m in months[i*3:i*3 3]:
combined.append( m " is in the season " s )
print(combined)
Output:
['January is in the season Winter', 'February is in the season Winter', 'March is in the season Winter', 'April is in the season Spring', 'May is in the season Spring', 'June is in the season Spring', 'July is in the season Summer', 'August is in the season Summer', 'September is in the season Summer', 'October is in the season Fall', 'November is in the season Fall', 'December is in the season Fall']
CodePudding user response:
months[0:2] , "is in the season " , seasons[3]
is not the correct way to construct a string.
You seem to have been confused by the behavior of print()
Instead, you want to construct a string first, and then append()
that string to your list.
def make_string(months, season):
p1 = ", ".join(months)
return p1 " is in the season " season
Here, we create p1
by joining all the elements of the sequence months
with a comma. Then, we append the string literal and the season to it, and return that value. To use this function:
s = make_string(months[0:2], seasons[3])
combined.append(s)
s = make_string(months[3:5], seasons[0])
combined.append(s)
s = make_string(months[6:8], seasons[1])
combined.append(s)
s = make_string(months[9:11], seasons[2])
combined.append(s)
Which gives:
combined = ['January, February is in the season Winter',
'April, May is in the season Spring',
'July, August is in the season Summer',
'October, November is in the season Fall']
Note that you don't need a loop because you never use the loop variable