+ 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?
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))