Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+2 votes

I am lost again. I was wondering why when I run this it says that NameError: name 'list1' is not defined.

def setupRectangle():

x1 = 0
y1 = 0
x2 = 100
y2 = 250
list1 = []
for i in range(9):
    rect1 = Rectangles(x1,y1,x2,y2)
    list1.append(rect1)
    x1 += 100
return list1


    
    

def main():

win = GraphWin("Shut The Box",WINDOW_WIDTH, WINDOW_HEIGHT)
win.setBackground("green")
rect = setupRectangle()
for i in range(len(list1)):
    list1(i).draw(win)
asked in CSC201 Spring 2021 by (2.7k points)

5 Answers

+3 votes

I believe rect1 should equal: Rectangle(Point(x1,y1), Point(x2,y2)), with the points being corners diagonal of one another.

answered by (5.3k points)
+2 votes

You get this error because you use the variable list1 in the main function but you haven't defined it. I believe that list1 that you want to use in the main function is the list which is returned in the setupRectangle function. So there is 2 ways to fix this error.

The first solution is to change rect = setupRectangle() to list1 = setupRectangle().

The second solution is to change all the variable list1 to rect. For example:

for i in range(len(rect)):

I would also recommend using better variable names. For example, in the setupRectangle function, instead of list1, it would be better to use rectList.

answered by (4.2k points)
edited by
+1

When I change it to list1 = setupRectangle(). This error occurs AttributeError: 'list' object has no attribute 'draw'

+1

Yes, you cannot draw a list but you can draw a rectangle which is an element of the list. So, the last two lines should be:

for i in range(len(list1)):
    list1[i].draw(win)

or

for rect in range(len(list1)):
    rect.draw(win)

I personally think that the second way is better.

+1 vote

I am not sure if this will fix the error but your list1(i).draw(win) should be list1i

answered by (2.3k points)
+1 vote

In the last line shouldn't it be list1.draw(win)
Instead of list1(i).draw(win)

answered by (4.4k points)
+1 vote

Your setupRectangle() returns a list and you set the variable rect to be equal to that list. So instead of list in the main, it should be rect.

answered by (1.8k points)
...