1

This is the simplest of exercises. I just don't understand why it wont work.

Here's my code:

hobbies = []
for i in range(3):
    hobby = raw_input("Name a hobby")
    hobbies = hobbies.append(hobby)

Basically I want to ask my user 3 times to name one of his hobbies, and store them in a list. But for some reason I'm getting this error,

Traceback (most recent call last):
  File "C:/Python27/hobbies.py", line 4, in <module>
    hobbies = hobbies.append(hobby)
AttributeError: 'NoneType' object has no attribute 'append'

which I don't really understand.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
mdemont
  • 107
  • 1
  • 4
  • 12

2 Answers2

3

The problem is that append() will change the list in-place. And when you call this function no value is returned.

The first time you get a None value for the variable hobbies. The second time you try to call the append() method for a None value...

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
0

You should not use hobbies = hobbies.append(). Instead use hobbies.append() only.

Vinay
  • 325
  • 1
  • 8