Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialScott Cannon
628 PointsI'm getting an error that is 'tuple' object has no attribute 'append'. How do I fix?
I keep getting this error when trying to finish the random_game challenge: Traceback (most recent call last): File "random_game.py", line 21 in <module> gussed_num.append(player_num) AttributeError: 'tuple' object has no attribute 'append'
Can some one help me figure out what I'm doing wrong?
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsAre you initializing guessed_num
as a tuple verses an empty list? See the difference in the ipython session below:
In [43]: guessed_num = () # declared as a tuple
In [44]: guessed_num.append(4) # this will fail
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-44-7978f7a77164> in <module>()
----> 1 guessed_num.append(4)
AttributeError: 'tuple' object has no attribute 'append'
In [45]: guessed_num = [] # declared as a list
In [46]: guessed_num.append(4) # append at will!
In [47]: guessed_num
Out[47]: [4]
Scott Cannon
628 PointsThank you for that, I figured it out, it was the square brackets. I was not declaring as a list, once I saw Chris's code, I saw it right away. They didn't cover that well in the video's or I missed it. Thanks the code now works!
Ahmed Elsawey
Courses Plus Student 3,527 PointsA tuple is a list which cannot be rewritten once created so it cannot be modified at all. and you have used append() which adds some data to the tuple therefore there is this error. Instead of using a tuple declare it as a list using the [] square brackets.
Tristan Gaebler
6,204 PointsTristan Gaebler
6,204 PointsCan I see some code form line 21