【发布时间】:2015-03-10 23:59:34
【问题描述】:
height, width, device = 99, 99, 99
num_pieces = 3
#create datastructure to hold piece dimensions*
pieces = [[height, width, device]]
for i in range(num_pieces - 1):
pieces = pieces.append([0,0,0])
print "initial list"
print pieces
#get dimentions for the rest of the list starting at second element*
for i in pieces[1: ]:
print "list in loop"
print pieces
# get height of the ith piece
print "please enter the height of piece %d" % (pieces.index(i)+1)
height = float(raw_input(">>"))
# get width of the ith piece
print "please enter the width of piece %d" % (pieces.index(i)+1)
width = float(raw_input(">>"))
# get device of the ith piece
print "please enter the hanging device of piece %d" % (pieces.index(i)+1)
device = float(raw_input(">>"))
# test if element is empty
if i == [0,0,0]:
i = [height, width, device]
#view completeled list of dimensions
print pieces
我认为我的 .append 有问题(我不应该使用 .extend 吗?)
这是我的错误。我知道[1: ] 也有问题,但首先我想弄清楚如何解决 .append 问题。
File "l_long.py", line 36, in <module>
for i in pieces[1: ]:
TypeError: 'NoneType' object has no attribute '__getitem__'
【问题讨论】:
-
请修复代码中的缩进,然后我们可以专注于其他各种问题。此外,如果您向我们提供一些您希望此代码打印的典型输出,那就太好了。
-
你为什么用这个???
pieces.index(i)+1可以直接使用pieces[0][i]
标签: python list loops append typeerror