-1

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 '='?
  • This makes no sense. The function `b1_click` returns its argument, `b`, which is the button it is called from. The function never actually uses it so why not just `command=b1_click` and wherever you use `pb1` just use the button you meant... – Tomerikoo Apr 10 '23 at 13:23
  • see: https://stackoverflow.com/questions/45337189/can-you-assign-variables-in-a-lambda – JonSG Apr 10 '23 at 13:23
  • it's a simple problem, you're using the wrong operator to sign the variable in the lambda function, or you can not do that and just return it – BGOPC Apr 10 '23 at 13:36
  • For your case, it is better to use a normal function instead of `lambda`. – acw1668 Apr 10 '23 at 15:17

1 Answers1

1

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.

BGOPC
  • 201
  • 9