另一个函数中的“ NameError:未定义名称'mousex'”

时间:2019-11-30 03:38:08

标签: python global-variables nameerror

我正在制作一个拉米纸牌游戏,但是在mouse[]cardx[]cardy[]周围存在一些问题。 我在程序开始时已将所有变量全局化,但是在子例程()中,似乎忘记了它们是什么。

def image(img,imgx,imgy):   
    screen.blit(img, (imgx,imgy))

def getmouse():
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    mousex,mousey=mouse[0],mouse[1]
    print("Mouse x:y",mousex,mousey, "Mouse Click",click[0]) #works fine here

def movableimgs():
    getmouse()
    for d in range(1,6):
        print("Mouse x:y",mousex,mousey, "Mouse Click",click[0])  #Error here nameError: name 'mousex'is not defined
        if cardx[d]<mousex>cardx[d]+71 and cardy[d]<mousey>cardy[d]+96:
            drag=1
        if click[0]==0:
            drag=0

我也遇到click[]的问题。它可以在getmouse()中使用,但是从getmouse()返回后便无法使用。我也遇到cardx[]cardy[]的问题。我在程序开始时就具有全局性。

2 个答案:

答案 0 :(得分:1)

全局变量和局部变量

来自代码/功能

def getmouse():
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    mousex,mousey=mouse[0],mouse[1]
    print("Mouse x:y",mousex,mousey, "Mouse Click",click[0]) #works fine here

您将mouseclickmousexmousey设置为local变量,则需要global命令来访问设置功能,所以您的getmouse

所以您的代码将是这样

def getmouse():
    global mouse, click, mousex, mousey
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    mousex,mousey=mouse[0],mouse[1]
    print("Mouse x:y",mousex,mousey, "Mouse Click",click[0]) #works fine here

但这不是有效的方法,因此您应该将getmouse函数更改为

def getmouse():
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    mousex,mousey=mouse[0],mouse[1]
    print("Mouse x:y",mousex,mousey, "Mouse Click",click[0]) #works fine here
    globals().update(locals()) #this is the effective way, and pythonic

因此您的完整代码是

def image(img,imgx,imgy):   
    screen.blit(img, (imgx,imgy))

def getmouse():
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    mousex,mousey=mouse[0],mouse[1]
    print("Mouse x:y",mousex,mousey, "Mouse Click",click[0]) #works fine here
    globals().update(locals())

def movableimgs():
    getmouse()
    for d in range(1,6):
        print("Mouse x:y",mousex,mousey, "Mouse Click",click[0])  #Error here nameError: name 'mousex'is not defined
        if cardx[d]<mousex>cardx[d]+71 and cardy[d]<mousey>cardy[d]+96:
            drag=1
        if click[0]==0:
            drag=0

答案 1 :(得分:0)

局部变量

mousexmouseyclick是被调用方函数getmouse中的局部变量。

要在调用方函数movableimgs中使用它,应返回被调用方函数中的值,然后将这些值解压缩为调用方函数中的变量。

def getmouse():
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    mousex,mousey=mouse[0],mouse[1]
    print("Mouse x:y",mousex,mousey, "Mouse Click",click[0])
    return mousex, mousey, click  # Return the values

def movableimgs():
    mousex, mousey, click = getmouse()  # Unpack the values into variables
    ...

全局变量

  

围绕mouse[]cardx[]cardy[]的问题。

  

click[]的问题...我在程序开始时就具有全局性。

要在函数中分配全局变量mouseclick,您需要将它们声明为global

def getmouse():
    global mouse, click  # Declare global variables
    mouse=pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()
    ...
相关问题