'Error: Append() takes exactly one argument (3 given)
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
Solution 1:[1]
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']
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
| Solution 2 | Pranav Hosangadi |
