+ 2

Walrus Operator Quick Question

print(x := int(input()) + y := int(input())) with this code, I'm simply trying to take two numbers as input then output the sum. This code fails and gives a SyntaxError. Is this error because I can't have multiple ":=" in the arguments section?

17th Jul 2022, 6:07 PM
Zane Al-Hamwy
Zane Al-Hamwy - avatar
1 Respuesta
+ 3
# Hi, Zane Al-Hamwy ! # You can have several ”Walrus operators” in the same expression, but you need parentheses around every Walrus operation: print(x := int(input()) + (y := int(input()))) # Then, in your code, you never use the advantage of using the Walrus operator, like this: print((x := int(input())) + (y := int(input())), "and", y*str(x) + x*str(y))
17th Jul 2022, 6:15 PM
Per Bratthammar
Per Bratthammar - avatar