如何将返回值存储在变量中并使用打印函数显示返回的值

2024-09-28 21:23:36 发布

您现在位置:Python中文网/ 问答频道 /正文

修改main函数以在win.getMouse软件()在while循环内部调用。将返回值存储在名为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

Tags: mainactionmsgwindrawdogelifwhile
1条回答
网友
1楼 · 发布于 2024-09-28 21:23:36

你的代码有很多问题(很明显)。在

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)     

这些都没有定义。在

相关问题 更多 >