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 trial

Python Python Basics (Retired) Pick a Number! Any Number! The Solution

Scott Cannon
Scott Cannon
628 Points

I'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?

Can I see some code form line 21

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Are 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
Scott Cannon
628 Points

Thank 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
PLUS
Ahmed Elsawey
Courses Plus Student 3,527 Points

A 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.