Do I read this code wrong?

#####Python #### What’s the result of this code? triple = lambda x: x * 3 add = lambda x, y: x + y print(add(triple(3, 4)) ######In this line of code I entered the value’s of x in the first two lines. So I get 3*3=9 for the first x and for the second x I was pondering for awhile. To calculate that I need the value of y and I couldn’t find any value for that in the code so I assumed that it would return as a 0. Therefore for the second x I came up with 4+0=4. But what I don’t get is why the code continues as 9+4.. Because I would have read 9*4.. https://code.sololearn.com/W8PUh7zJ1kQZ/?ref=app #####

8/20/2018 5:50:02 PM

Wilson Chong

2 Answers

New Answer


Wilson Chong I think we have a problem with that code, now I understand your confusion, the 'triple' expected one argument passed, but two was passed 'triple(3,4)'. I guess the correct form of the code should be along these lines: triple = lambda x: x * 3 add = lambda x, y: x + y print(add(triple(3), 4)) Here, as you see, 'triple' is called with 1 argument (3), and the 'add' is called with 2 arguments, first argument is return value of 'triple(3)' which is 9, and second argument is 4. This outputs 13 (9+4). Hth, cmiiw


Thanks, now I get the code!