thevamp
thevamp

Reputation: 11

Adding an integer variable to a list

if robotwalk <= 1 or robotwalk >= 8:
    robotfall +=1
    print"robotfalltrue"
    robotwalk=3.5
    robotlist =robotlist.append( robotsteps )
    robotsteps=0
    print robotlist

my question is is that how do fix this: i keep getting an error. robotlist =robotlist.append( robotsteps ). robot list has been defined as robotlist=[]

error: AttributeError: 'NoneType' object has no attribute 'append'

Upvotes: 1

Views: 57

Answers (3)

A.J. Uppal
A.J. Uppal

Reputation: 19274

The method .append() modifies the list in-place, and therefore returns nothing, hence None. Instead, just don't assign the output of .append() to any variable and the code will work like a charm:

if robotwalk <= 1 or robotwalk >= 8:
    robotfall +=1
    print"robotfalltrue"
    robotwalk=3.5
    robotlist.append( robotsteps )
    robotsteps=0
    print robotlist

Upvotes: 1

Darren Stone
Darren Stone

Reputation: 2068

No re-assignment. Simply do this:

robotlist.append(robotsteps)

Or, alternately:

robotlist += [robotsteps]

But I think the first is more clear.

Upvotes: 0

arshajii
arshajii

Reputation: 129577

append() modifies the list in-place and returns None. Therefore, all you need is

robotlist.append(robotsteps)

without the assignment.

Upvotes: 2

Related Questions