Liste hat kein Attribut len()
Verfasst: Donnerstag 20. Oktober 2011, 23:41
Hi Leuts. Könnt ihr mir bei folgendem Problem helfen?
gibt mir
aus. Warum hat self.numbers kein len(), eine Zeile drüber wird sie aber ausgegeben?
Code: Alles auswählen
from random import choice
class puzzle(object):
def __init__(self):
self.numbers = [0,1,2,3,4,5,6,7,'#']
self.cords= [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2],[2,0],[2,1],[2,2]]
def randomize(self):
print len(self.numbers)
a = 0
while(len(self.numbers) > 0):
rand = choice(self.numbers)
self.cords[a].append(rand)
a = a+1
self.numbers = self.numbers.remove(rand)
def printModule(self):
i = 2
for a in range (len(self.cords)):
print self.cords[a],
if (a == i):
print "\r"
i = i+3
puz = puzzle()
puz.randomize()
puz.printModule()
Code: Alles auswählen
9
Traceback (most recent call last):
File "D:/Python27/8-erspiel.py", line 30, in <module>
puz.randomize()
File "D:/Python27/8-erspiel.py", line 14, in randomize
rand = choice(self.numbers)
File "D:\Python27\lib\random.py", line 274, in choice
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
TypeError: object of type 'NoneType' has no len()