+ 2
def power(x, y): if y == 0: return 1 else: return x * power(x, y-1) print(power(2, 3))
обьясните пожалуйста, как в самом конце, после того как 0 ==0 retёrn - нулась 1, и перешла вверх, вот это: 2 * (2, 1) как "два умножить на скобка открывается два запятая один скобка закрывается" вообще решается ??????? только это не понятно или тут в самом конце retër - нутая единичка заполняет всю скобку вот так: 2 * (1) ???
7 Answers
+ 16
def power(x, y):
if y == 0:
return 1
else:
return x * power(x, y-1)
first function call:
power(2, 3)
x = 2
y = 3
y == 0 ? false
=> return 2 * power(2, 2)
temporary result: 2 * power(2, 2)
second function call:
power(2, 2)
x = 2
y = 2
y == 0 ? false
=> return 2 * power(2, 1)
temporary result: 2 * (2 * power(2, 1))
third function call:
power(2, 1)
x = 2
y = 1
y == 0 ? false
=> return 2 * power(2, 0)
temporary result: 2 * (2 * (2 * power(2, 0)))
fourth function call:
power(2, 0)
x = 2
y = 0
y == 0 ? true
=> return 1
result: 2 * (2 * (2 * 1)) = 8
+ 9
I hope you understand English
In the above function
X is the base
Y is the power
Eg: 2^3 = power(2,3) = 2*2*2 = 8
5^2 = power (5,2) = 5*5 = 25
9^2 = power (9,2) = 9*9 =81
2 isn't multiplied with (2,1), but with the return value of power (2,1)
It works like this
2 * power(2,2)
2 * 2 * power(2,1)
2 * 2 * 2
=8
+ 4
Thanks all!!! Really i appreciate you guys every one!!!! Thanks that having time for me 😘😘😘
+ 3
power(2, 3) =
2 * power(2, 2) =
2 * 2 * power(2, 1) =
2 * 2 * 2 * power(2, 0) =
2 * 2 * 2 * 1
+ 1
i mean:
how in the end, after the 0 ==0 and retеrns 1, and moved up, that is:
2 * (2, 1)
how "two multiplied by open round bracket two comma one round bracket close" at all is solved ???????
or here at the end retern 1- mentioned fills the entire bracket here:
2 * (1)
???
+ 1
8
0