[英]How to call a function inside another function in python
def print_money(n1, n2):
print('hello world')
money_sum = adds_numbers(n1, n2)
print(money_sum)
def adds_numbers(n1,n2):
print_money(n1,n2)
adds_numbers(2,3)
I am trying to call a function from another function, why is the result giving an infinite loop.我正在尝试从另一个 function 调用 function,为什么结果会出现无限循环。
Because your print_money
function is calling yet another adds_number
function, which in turns call yet another print_money
function, and will keep on repeating this loop forever.因为您的
print_money
function 正在调用另一个adds_number
function,它又会调用另一个print_money
function,并将永远重复此循环。
Sidenote: I think what you want to do is旁注:我认为你想要做的是
def adds_numbers(n1, n2):
return n1 + n2 // Returns the sum of both numbers, as implied by the function name
Change add_numbers to return n1+n2, that fixes the code.更改 add_numbers 以返回 n1+n2,从而修复代码。
def print_money(n1, n2):
print('hello world')
money_sum = adds_numbers(n1, n2)
print(money_sum)
def adds_numbers(n1,n2):
return n1+n2
adds_numbers(2,3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.