英文:
I want to pass in 2 things for a function but one is in another function and the function which I need the return is called in a lambda. What do I do?
问题
以下是翻译好的部分:
我需要为一个函数传递2个额外参数,但是参数 b - 只能是 b1、b2 或 b3 - 位于一个函数后面,但它在一个 lambda 内部调用,所以当我尝试创建一个新变量 - pb1 - 时,它不起作用。
我期望它能够正常工作,并且被按下的按钮 - pb1 - 被传递到 checkwin,然后传递到 checkwinpos,但它没有工作。错误消息如下:
b1 = Button(root, text="Rock", height=3, width=6, bg="SystemButtonFace", command=lambda: [pb1 = b1_click(b1)])
^^^^^^^^^^^^^^^^^^
SyntaxError: 无效的语法。也许你想要使用 '==' 或 ':=' 而不是 '='?
英文:
I need to pass in 2 more parameters for a function but b - which can only be b1, b2 or b3 - is behind a function but it is called inside of a lambda so when I try and make a new variable - pb1 - it doesn't work.
def b1_click(b):
global p1
p1 = p1.join(b["text"])
b4.config(state=ACTIVE)
b5.config(state=ACTIVE)
b6.config(state=ACTIVE)
b1.config(state=DISABLED)
b2.config(state=DISABLED)
b3.config(state=DISABLED)
return b
def b2_click(b):
global p2
p2 = p2.join(b["text"])
b4.config(state=DISABLED)
b5.config(state=DISABLED)
b6.config(state=DISABLED)
def checkwin(b, bb):
possibilities = [{"Rock", "Scissors"}, {"Scissors", "Paper"}, {"Paper", "Rock"}]
for possibility in possibilities:
checkwinpos(*possibility, b, bb)
def checkwinpos(win, lose, b, bb):
if b["text"] == win and bb["text"] == lose:
b.config(bg="Green")
bb.config(bg="Red")
elif bb["text"] == win and b["text"] == lose:
bb.config(bg="Green")
b.config("Red")
else:
messagebox.showinfo("Jensen The Great's Rock Paper Scissors", "You both picked the same thing")
root = Tk()
root.title("Jensen The Great's Rock Paper Scissors")
root.geometry("1200x710")
b1 = Button(root, text = "Rock", height = 3, width = 6, bg="SystemButtonFace", command=lambda: [pb1 = b1_click(b1)])
b2 = Button(root, text = "Paper", height = 3, width = 6, bg="SystemButtonFace", command=lambda: [pb1 = b1_click(b2)])
b3 = Button(root, text = "Scissors", height = 3, width = 6, bg="SystemButtonFace", command=lambda: [pb1 = b1_click(b3)])
b4 = Button(root, text = "Rock", height = 3, width = 6, bg="SystemButtonFace", command=lambda: [b2_click(b4), checkwin(pb1, b4)])
b5 = Button(root, text = "Paper", height = 3, width = 6, bg="SystemButtonFace", command=lambda: [b2_click(b5), checkwin(pb1, b5)])
b6 = Button(root, text = "Scissors", height = 3, width = 6, bg="SystemButtonFace", command=lambda: [b2_click(b6), checkwin(pb1, b6)])
b4.config(state=DISABLED)
b5.config(state=DISABLED)
b6.config(state=DISABLED)
I was expecting it to work and the button which was pressed - pb1 - to be passed into checkwin and then passed into checkwinpos but it didn't work. The error message is here:
b1 = Button(root, text = "Rock", height = 3, width = 6, bg="SystemButtonFace", command=lambda: [pb1 = b1_click(b1)])
^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
答案1
得分: 1
是的,错误消息是正确的。等号 ' = ' 是一个赋值运算符,不能在 lambda 函数内使用,因为它不是一个有效的表达式。
要修复这个错误,你可以在 lambda 函数内用等于运算符 ' == ' 替换等号 ' = '。下面是修正后的代码:
b1 = Button(root, text="Rock", height=3, width=6, bg="SystemButtonFace", command=lambda: [b1_click(b1)])
或者,你可以在 lambda 函数内使用獭运算符 ':=' 代替等号 ' = ',就像这样:
b1 = Button(root, text="Rock", height=3, width=6, bg="SystemButtonFace", command=lambda: [pb1 := b1_click(b1)])
不过,请注意,獭运算符只在 Python 3.8 或更高版本中可用。
英文:
Yes, the error message is correct. The equal sign '=' is an assignment operator and cannot be used within a lambda function, as it is not a valid expression.
To fix the error, you can replace the equal sign '=' with the equal-to operator '==' within the lambda function. Here's the corrected code:
b1 = Button(root, text="Rock", height=3, width=6, bg="SystemButtonFace", command=lambda: [b1_click(b1)])
Alternatively, you could use the walrus operator ':=' instead of the equal sign '=' within the lambda function, like this:
b1 = Button(root, text="Rock", height=3, width=6, bg="SystemButtonFace", command=lambda: [pb1 := b1_click(b1)])
However, note that the walrus operator is only available in Python 3.8 or higher.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论