Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这个循环是怎么回事?_Python_Loops_For Loop - Fatal编程技术网

Python 这个循环是怎么回事?

Python 这个循环是怎么回事?,python,loops,for-loop,Python,Loops,For Loop,我试图创建一行正方形,其数量由用户输入决定。 我希望每个正方形的x坐标乘以循环运行的次数,再乘以每个正方形的宽度。 到目前为止,它似乎要么只画一个正方形,要么我给draw函数提供了每个循环相同的坐标。 有人能发现我的错误吗 def main(): seed() # Initialize random number generator top_left_x = 100 top_left_y = 100 width = 60 height = 60

我试图创建一行正方形,其数量由用户输入决定。 我希望每个正方形的x坐标乘以循环运行的次数,再乘以每个正方形的宽度。 到目前为止,它似乎要么只画一个正方形,要么我给draw函数提供了每个循环相同的坐标。 有人能发现我的错误吗

def main():
    seed()  # Initialize random number generator

    top_left_x = 100
    top_left_y = 100
    width = 60
    height = 60
    y_num = 0
    # num_rows = int(input('Number of rows: '))  # commented out for now
    num_columns = int(input('Number of columns: '))



    for i in range (num_columns):
        window = GraphWin('Lab 3B', 800, 800)
        new_x = i * width + top_left_x
        new_y = top_left_y
        top_left_point = Point(new_x, new_y)
        bottom_right_point = Point(new_x + width, new_y + height)
        enclosing_rectangle = Rectangle(top_left_point, bottom_right_point)
        enclosing_rectangle.setFill(random_color())
        enclosing_rectangle.draw(window)

    for i in range(10):
        c_point = window.getMouse()
        x_c_point = c_point.getX()
        y_c_point = c_point.getY()
        print('x =', x_c_point, 'y =', y_c_point)

    window.getMouse()
    window.close()
这将在每次循环中重新创建窗口。像这样交换前两行:

window = GraphWin('Lab 3B', 800, 800)
for i in range (num_columns):
    new_x = i * width + top_left_x
但是,下一个问题是您也只有一个矩形,因此您需要保存您创建的每个矩形,因此这应该可以工作:

window = GraphWin('Lab 3B', 800, 800)
rects = [] # Stores rectangles
for i in range (num_columns):
    new_x = i * width + top_left_x
    new_y = top_left_y
    top_left_point = Point(new_x, new_y)
    bottom_right_point = Point(new_x + width, new_y + height)
    enclosing_rectangle = Rectangle(top_left_point, bottom_right_point)
    enclosing_rectangle.setFill(random_color())
    enclosing_rectangle.draw(window)
    rects.append(enclosing_rectangle) # Keep this rectangle

好的,是的,这很有道理,谢谢!它仍然只画了一个正方形。好吧,我已经更新了我的答案;在程序运行期间,您需要保留所有的
矩形
对象
window = GraphWin('Lab 3B', 800, 800)
rects = [] # Stores rectangles
for i in range (num_columns):
    new_x = i * width + top_left_x
    new_y = top_left_y
    top_left_point = Point(new_x, new_y)
    bottom_right_point = Point(new_x + width, new_y + height)
    enclosing_rectangle = Rectangle(top_left_point, bottom_right_point)
    enclosing_rectangle.setFill(random_color())
    enclosing_rectangle.draw(window)
    rects.append(enclosing_rectangle) # Keep this rectangle