0

return x * power(x, y-1)?

def power(x, y): if y == 0: return 1 else: return x * power (x, y-1) print(power(2, 3)) Output: 8 #y is not equal to 0, so I run else # return 2 * power(2, 3-1) __evaluates to__ 2 * power (2, 2) #How did they get print(power(2, 3))? Was I just to ignore the top portion of the code? #Even then, shouldn't it say something like 2 *= 3? Where was what I was supposed to do with the (2, 3) defined? Thanks for any help;)).

12th Apr 2019, 9:42 PM
tristach605
tristach605 - avatar
1 Resposta
0
power(2, 3) # y == 3 == 2 * power(2, 3-1) == 2 * power(2 , 2) # y == 2 == 2 * 2 * power(2, 2-1) == 4 * power(2, 1) # y == 1 == 4 * 2 * power(2, 1-1) == 8 * power(2, 0) # y == 0 == 8 * 1 == 8.
13th Apr 2019, 1:18 AM
Diego
Diego - avatar