+ 1

why error: s = lambda a: a=4

The conditions of the assignment from the "code challenges": What is the output of this code? s = lambda a: a=4 print(s(2)) This is a question from the local mini-games "code challenges" and the correct answer is an error. My level of knowledge so far does not allow me to understand why exactly the error, read a few other resources and still do not understand. Hope for your help

14th Mar 2023, 5:10 PM
qkue
5 Réponses
+ 3
It is a question from the challenges (play against other users). Lambda functions can only contain expressions. An assignment is not an expression.
14th Mar 2023, 7:56 PM
Lisa
Lisa - avatar
+ 4
If you want to check if the parameter to the lambda is equal to 4, you should use ==
14th Mar 2023, 5:25 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Can you specify which course and which lesson? Or give a complete description of the task and your full solution.
14th Mar 2023, 6:28 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Not sure which python they are running here, but as of 3.8 you can use the walrus operator( :=) inside a lambda. So I would try changing it to s = lambda a: a := 4
15th Mar 2023, 1:08 AM
Bob
0
The conditions are defined by the game "code challenges"
14th Mar 2023, 6:24 PM
qkue