Question

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')

Above is the basic function, then I am asked to do a copy function to keep track of the original A.

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

Then I tried to printBoard(newA) and here comes the error:

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

Can someone please tell me why this is an error?

Was it helpful?

Solution 2

You aren't copying the list correctly, I think.

Your original list looks something like this:

[[1,2,3],[4,5,6],[7,8,9]]

When you copy it, you create a new list called newA:

[]

and you just add elements to it:

[1,2,3,4,5,6,7,8,9]

so your list format is different.

This is perhaps what you intended:

newA=[]
row=[]
for row in range(0,height):
    newRow = []
    for col in range(0,width):
        if A[row][col]==0:
           newRow+=[0]
        elif A[row][col]==1:
            newRow+=[1]
    newA += [newRow]

OTHER TIPS

Here is my solution, which I tested:

def copy(a):
    return [row[:] for row in a]

If this is not homework, use copy.deepcopy():

import copy
b = copy.deepcopy(a)

Your function copy is incorrect. This code:

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

a = [
  [1, 1, 0],
  [0, 1, 1],
]

print a

print copy(a)

prints this:

[[1, 1, 0], [0, 1, 1]]
[1, 1, 0, 0, 1, 1]

As you see it doesn't contain sublists, so it tries to iterate integers.

I thing using copy.deepcopy would do the job.

You have not provided enough code to reproduce the problem, but I can tell you what that error means. It means that, in the expression for col in row:, row is actually an int rather than some kind of iterable type.

Tried running this and noticed that there is a comma at the end of increment statement in CreateBoard:

row += createOneRow(width),

This is missing in CopyA method when incrementing newA. I tried adding a comma to these lines and there is no error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top