0

我的代码有点问题,基本上我需要创建图像中显示的模式

模式 1

使用graphicsJohn Zelle 教授编写的模块(可在此处获得)。

这是 10 条直线,需要根据用户选择在不同大小的画布中创建它。所以我决定循环它并在每行之间添加额外的空间,但是当我执行循环时,我得到一个错误,说这条线已经被绘制了,有人知道如何避免这种情况吗?

from graphics import *

def Main():

    valid = False
    while not valid:

        sizeInput= eval(input("PLease select a size, 7, 9, 11: "))
        if sizeInput == 7 or sizeInput == 9 or sizeInput == 11 :
            size= sizeInput*100
            valid=True

        else:
            print("Invalid Number")

    Draw(size)

def Draw(size):
    win = GraphWin("Pat1",size,size)
    PointX= Point(0,0)
    PointY= Point(700,70)

    line = Line(PointX,PointY)
    i = 0
    while i<10:
        line.draw(win)
        PointX.x+70
        PointY.y+70
        i=i+1
4

1 回答 1

0

作为一个 Python 新手,我尝试了这个项目。这就是我想出的。我确信需要进行优化并且需要添加错误检查,但它确实有效。

from graphics import *

def Main():
    # Have user enter actual window size (ex: "500" for a 500x500 window)
    size = int(input("Please enter a window size: "))
    # Have user enter number of lines to draw
    line_count = int(input("Please enter a line count: "))

    # Create the window
    win = GraphWin("Pat1", size, size)
    # Calculate space between lines
    step = size / line_count

    # Renamed these to something more logical
    point_start = Point(0, 0)
    point_end = Point(size, step)

    # Draw top half
    for i in range(line_count):
        line = Line(point_start, point_end)
        line.draw(win) 
        point_start.x += step
        point_end.y += step

    point_start = Point(0, 0)
    point_end = Point(step, size)

    # Draw bottom half
    for i in range(line_count):
        line = Line(point_start, point_end)
        line.draw(win) 
        point_start.y += step
        point_end.x += step

Main()
于 2017-11-22T19:13:58.523 回答