+ 1

Help please! Can someone help me understand this code?

x = 5 def fun(x): x = x - (x-2) return x print(fun(fun(fun(x-1))))

31st Aug 2021, 7:56 PM
Jenkins
3 Réponses
+ 3
Inner function( fun(x-1) )is called first which returns 2 . value 2 becomes argument for another inner function (fun(2)) and this is how it goes on ..
31st Aug 2021, 8:13 PM
Abhay
Abhay - avatar
+ 3
You should start with the innermost function fun(x-1) and put for x 5. That mesns fun(5-1) = fun(4). So from definition of the function: x= 4 - (4 - 2) = 4 - 2 = 2 So this return 2. When you put this 2 into next function fun you get: fun(2) = 2 - (2 - 2) = 2 and so on, and so on. For this reason the result is 2.
31st Aug 2021, 8:29 PM
JaScript
JaScript - avatar
+ 2
What is pcep?
31st Aug 2021, 8:27 PM
Simon Sauter
Simon Sauter - avatar