为什么这是语法错误?使用屏幕点击时有没有办法返回这两个变量?
这是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改为
您正在将变量声明为全局变量并同时为其赋值。 Python 希望您将变量声明为全局变量,然后您可以为其赋值。
W3school-Python 全局变量
Change to
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