使用 return 值多次调用一个函数
Calling a function multiple times with return value
from graphics import *
def draw():
returnStuff = {'again' : 0, '1st' : 1 }
draw.again = False
win = GraphWin("Quadrilateral Maker", 600, 600)
win.setBackground("yellow")
text = Text(Point(150, 15), 'Click 4 points to create a Quadrilateral')
text.draw(win)
#gets the 4 points
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
p4 = win.getMouse()
p4.draw(win)
vertices = [p1, p2, p3, p4]
#draws the shape
quad = Polygon(vertices)
quad.setFill('red')
quad.setOutline('black')
quad.setWidth(3)
quad.draw(win)
text.setText('Click in the appropriate box.')
#Quit box
quitBox = Rectangle(Point(30, 500), Point(100,550))
quitBox.setFill('green')
quitBox.draw(win)
quitorNah = Text(Point(60, 490), 'Quit')
quitorNah.draw(win)
#again box
quitBox = Rectangle(Point(480, 500), Point(550,550))
quitBox.setFill('green')
quitBox.draw(win)
quitorNah = Text(Point(510, 490), 'Draw Again')
quitorNah.draw(win)
click = win.getMouse()
x = click.getX()
y = click.getY()
while True:
if 30 < x < 100 and 500 < y < 550:
returnStuff['again'] = 0
win.close()
break
elif 480 < x < 550 and 500 < y < 550:
returnStuff['again'] = 1
win.close()
break
return returnStuff
count = 1
returnValue = draw()
if returnValue['1st'] == 1:
count = 0
while count == 1 or returnValue['again'] == 1:
return_value = draw()
所以我有这个使用 Zelle 图形的简单交互式程序,它要求用户单击 window 中的 4 个点,然后从中创建一个形状。然后,向用户显示 2 个框,一个用于退出,一个用于再次绘制。我的抽奖再次无效,它与 return 值有关。我正在 return 字典,因为我需要访问函数中的两个变量。在 'returnStuff' 词典中,我有一个名为 'again' 的部分,它最初设置为 0。如果用户再次单击 运行 框,它会将此值更改为 1,然后在函数之外,我有一个 if 语句,如果那个值再次为 1,它应该再次调用该函数。它第一次正确地执行此操作,但是我的程序第二次完全停止,我不明白为什么。
谁能解释为什么会这样?
我想你需要一段时间...
while count==1 or returnValue['again'] == 1:
returnValue = draw()
from graphics import *
def draw():
returnStuff = {'again' : 0, '1st' : 1 }
draw.again = False
win = GraphWin("Quadrilateral Maker", 600, 600)
win.setBackground("yellow")
text = Text(Point(150, 15), 'Click 4 points to create a Quadrilateral')
text.draw(win)
#gets the 4 points
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
p4 = win.getMouse()
p4.draw(win)
vertices = [p1, p2, p3, p4]
#draws the shape
quad = Polygon(vertices)
quad.setFill('red')
quad.setOutline('black')
quad.setWidth(3)
quad.draw(win)
text.setText('Click in the appropriate box.')
#Quit box
quitBox = Rectangle(Point(30, 500), Point(100,550))
quitBox.setFill('green')
quitBox.draw(win)
quitorNah = Text(Point(60, 490), 'Quit')
quitorNah.draw(win)
#again box
quitBox = Rectangle(Point(480, 500), Point(550,550))
quitBox.setFill('green')
quitBox.draw(win)
quitorNah = Text(Point(510, 490), 'Draw Again')
quitorNah.draw(win)
click = win.getMouse()
x = click.getX()
y = click.getY()
while True:
if 30 < x < 100 and 500 < y < 550:
returnStuff['again'] = 0
win.close()
break
elif 480 < x < 550 and 500 < y < 550:
returnStuff['again'] = 1
win.close()
break
return returnStuff
count = 1
returnValue = draw()
if returnValue['1st'] == 1:
count = 0
while count == 1 or returnValue['again'] == 1:
return_value = draw()
所以我有这个使用 Zelle 图形的简单交互式程序,它要求用户单击 window 中的 4 个点,然后从中创建一个形状。然后,向用户显示 2 个框,一个用于退出,一个用于再次绘制。我的抽奖再次无效,它与 return 值有关。我正在 return 字典,因为我需要访问函数中的两个变量。在 'returnStuff' 词典中,我有一个名为 'again' 的部分,它最初设置为 0。如果用户再次单击 运行 框,它会将此值更改为 1,然后在函数之外,我有一个 if 语句,如果那个值再次为 1,它应该再次调用该函数。它第一次正确地执行此操作,但是我的程序第二次完全停止,我不明白为什么。
谁能解释为什么会这样?
我想你需要一段时间...
while count==1 or returnValue['again'] == 1:
returnValue = draw()