如何在变量中存储返回值并使用print显示返回的值

时间:2010-10-27 21:53:04

标签: python

修改main函数,在while循环内的win.getMouse()调用之后立即调用getAction。将返回值存储在名为action的变量中,并使用print显示返回的值。

到目前为止,这是我的代码。我遇到的问题是如何在名为action的变量中返回值,然后使用print显示返回的值。

ACTION_PET = 1
ACTION_FEED = 2
ACTION_PLAY = 3
ACTION_IGNORE = 4
ACTION_ERROR = 5

def getAction():

    win = GraphWin("CS1400 - Pet Dog", 610, 500) 
    clear_screen(win)                            
    rec1, rec2, rec3, rec4= draw_buttons(win)
        while True:
            mouseClick = win.getMouse()                  

            if inBox(rec1, mouseClick):
                ACTION_PET(win)                            
            elif inBox(rec2, mouseClick):
                ACTION_FEED(win)                            
            elif inBox(rec3, mouseClick):
                ACTION_PLAY(win)                         
            elif inBox(rec4, mouseClick):
                ACTION_IGNORE(win)
            else:
                ACTION_ERROR(win)

                break                  


# main program

def main():
    """dog drawing program"""

    win = GraphWin("CS1400 - Pet Dog", 610, 500)      # create graphics window
    clear_screen(win)                                # start with a clear screen
    rec1, rec2, rec3, rec4= draw_buttons(win)        # create user buttons

    # loop forever until cat is dead
    while True:
        mouseClick = win.getMouse()
                                                     # get mouse click

        if inBox(rec1, mouseClick):
            drawHappy(win)                            # draw happy dog
        elif inBox(rec2, mouseClick):
            drawAngry(win)                            # draw angry dog
        elif inBox(rec3, mouseClick):
            drawSleeping(win)                         # draw sleeping dog
        elif inBox(rec4, mouseClick):
            drawBored(win)                            # draw bored dog

            break



    # wait for user to click one more time before ending the program
    msg_location = Point(305, 430)
    msg = Text(msg_location, "Click anywhere to quit.")
    msg.setTextColor("red")
    msg.draw(win)                         # draw message

    win.close()
    return

1 个答案:

答案 0 :(得分:0)

你的代码有很多(显然)错误。

ACTION_PET(win)将为您提供TypeError: 'int' object is not callable

return the value in a variable named action什么价值?

if inBox(rec1, mouseClick): inBox()定义在哪里?


if inBox(rec1, mouseClick):
            drawHappy(win)                            # draw happy dog
        elif inBox(rec2, mouseClick):
            drawAngry(win)                            # draw angry dog
        elif inBox(rec3, mouseClick):
            drawSleeping(win)                         # draw sleeping dog
        elif inBox(rec4, mouseClick):
            drawBored(win)     

这些都没有定义。