def createOneRow(width):
""" returns one row of zeros of width "width"...
You should use this in your
createBoard(width, height) function """
row = []
for col in range(width):
row += [0]
return row
def createBoard(width,height):
"""creates a list
"""
row = []
for col in range(height):
row += createOneRow(width),
return row
import sys
def printBoard(A):
""" this function prints the 2d list-of-lists
A without spaces (using sys.stdout.write)
"""
for row in A:
for col in row:
sys.stdout.write(str(col))
sys.stdout.write('\n')
以上是基本功能,接下來我要求我做一個拷貝函數來跟蹤原始的A
。無法打印列表「int」對象不可迭代
def copy(A):
height=len(A)
width=len(A[0])
newA=[]
row=[]
for row in range(0,height):
for col in range(0,width):
if A[row][col]==0:
newA+=[0]
elif A[row][col]==1:
newA+=[1]
return newA
然後我試圖printBoard(newA)
和來這裏的錯誤:
Traceback (most recent call last):
File "<pyshell#37>", line 1, in <module>
printBoard(newA)
File "/Users/amandayin/Downloads/wk7pr2/hw7pr2.py", line 35, in printBoard
for col in row:
TypeError: 'int' object is not utterable
有人能告訴我這是爲什麼錯誤?
你是如何創建你正在複製的原始'A'的? – merlin2011
「TypeError:'int'object is not utterable」? 「用語言來」?真? – Johnsyweb