使用返回值

时间:2015-12-09 23:55:23

标签: function python-3.x dictionary return-value zelle-graphics

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图形,它要求用户点击一个窗口中的4个点,然后从中创建一个形状。然后,向用户显示2个框,一个用于退出,一个用于再次绘制。我的平局再次起作用,它与返回值有关。我正在返回一本字典,因为我需要访问函数中的两个变量。在&quot; returnStuff&#39;字典,我有一个名为&#39;再次&#39;,最初设置为0.如果用户再次点击运行框,它会将此值更改为1,然后在函数外部我有一个if语句应该再次调用该函数,如果该值再次为1.它在第一次正确地执行此操作,但是第二次围绕我的程序只是一起停止,我不明白为什么。

任何人都可以解释为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

我想你需要一段时间......

while count==1 or returnValue['again'] == 1:
    returnValue = draw()