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

11th May 2019, 9:51 AM
Магомед Акуев
Магомед Акуев - avatar
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
11th May 2019, 10:28 AM
Anna
Anna - avatar
+ 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
11th May 2019, 10:02 AM
‎ ‏‏‎Anonymous Guy
+ 4
Thanks all!!! Really i appreciate you guys every one!!!! Thanks that having time for me 😘😘😘
11th May 2019, 5:59 PM
Магомед Акуев
Магомед Акуев - avatar
+ 3
power(2, 3) = 2 * power(2, 2) = 2 * 2 * power(2, 1) = 2 * 2 * 2 * power(2, 0) = 2 * 2 * 2 * 1
11th May 2019, 10:33 AM
Игорь Яковенко
Игорь Яковенко - avatar
+ 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) ???
11th May 2019, 10:13 AM
Магомед Акуев
Магомед Акуев - avatar
+ 1
8
2nd Sep 2022, 11:45 AM
wessam lahmod nados
0
22nd Dec 2020, 11:55 PM
Saeed AL-Masoodi
Saeed AL-Masoodi - avatar