我的代码有点麻烦,基本上我需要创建图片中显示的模式
使用John Zelle教授撰写的graphics
模块(可用here)。
这是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
答案 0 :(得分: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()