为什么这是语法错误?使用屏幕点击时有没有办法返回这两个变量?

发布于 2025-01-10 21:47:35 字数 1176 浏览 3 评论 0原文

这是 3.10 中的一个 python 程序,我需要一个单独的 x 和 y 变量用于我的函数,这样我就可以将零/叉放在它们需要去的地方。我尝试将其作为一二返回,然后再拆分,但它实际上不存储任何内容。我的计划是将它们设置为全局变量,但它说这是一个语法错误。我仍在学习编码,但不明白为什么。另外,如果有一种方法可以返回它们,那是什么。

def which(hx,hy):
    if (hx > -54 and hx < -21) and (hy >43 and hy < 75):
        place=0
    elif (hx > -22 and hx < 19) and (hy >43 and hy < 75):
        place=1
    elif (hx > 20 and hx < -54) and (hy >43 and hy < 75):
        place=2
    elif (hx > -54 and hx < -21) and (hy > 3 and hy < 41):
        place=3
    elif (hx > -22 and hx < 19) and (hy >3 and hy < 41):
        place=4
    elif (hx > 20 and hx < -54) and (hy >3 and hy < 41):
        place=5
    elif (hx > -54 and hx < -21) and (hy > -37 and hy < 1):
        place=6
    elif (hx > -22 and hx < 19) and (hy > -37 and hy < 1):
        place=7
    elif (hx > 20 and hx < -54) and (hy > -37 and hy < 1):
        place=8
    return place

def seperate(x,y):
    global cx = x # this is the problem(it highlights the = red)
    global cy = y

onscreenclick(seperate)
print(cx)
print(cy)
h=which(cx,cy)
n(h)

This is a python program in 3.10 and I need a separate x and y variable to use for my function which so I can place the noughts /crosses where they need to go. I have tried returning it as one two then split later but it doesn't actually store anything. my plan was then to set them a s global variables but it says it is a syntax error. I am still learning to code and don't understand why. also if there is a way of returning them what is it.

def which(hx,hy):
    if (hx > -54 and hx < -21) and (hy >43 and hy < 75):
        place=0
    elif (hx > -22 and hx < 19) and (hy >43 and hy < 75):
        place=1
    elif (hx > 20 and hx < -54) and (hy >43 and hy < 75):
        place=2
    elif (hx > -54 and hx < -21) and (hy > 3 and hy < 41):
        place=3
    elif (hx > -22 and hx < 19) and (hy >3 and hy < 41):
        place=4
    elif (hx > 20 and hx < -54) and (hy >3 and hy < 41):
        place=5
    elif (hx > -54 and hx < -21) and (hy > -37 and hy < 1):
        place=6
    elif (hx > -22 and hx < 19) and (hy > -37 and hy < 1):
        place=7
    elif (hx > 20 and hx < -54) and (hy > -37 and hy < 1):
        place=8
    return place

def seperate(x,y):
    global cx = x # this is the problem(it highlights the = red)
    global cy = y

onscreenclick(seperate)
print(cx)
print(cy)
h=which(cx,cy)
n(h)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自←快乐 2025-01-17 21:47:35
def seperate(x,y):
    global cx = x # this is the problem(it highlights the = red)
    global cy = y

更改为

def seperate(x,y):
    global cx 
    cx= x # this is the problem(it highlights the = red)
    global cy
    cy = y

您正在将变量声明为全局变量并同时为其赋值。 Python 希望您将变量声明为全局变量,然后您可以为其赋值。

W3school-Python 全局变量

def seperate(x,y):
    global cx = x # this is the problem(it highlights the = red)
    global cy = y

Change to

def seperate(x,y):
    global cx 
    cx= x # this is the problem(it highlights the = red)
    global cy
    cy = y

You are declaring a variable as global and assigning it a value at the Same time. Python wants you to declare variables global, then you can assign values to it.

W3school-Python global variables

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文