通过递归添加 [英] Adding through recursion

查看:96
本文介绍了通过递归添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常简单的答案,但为什么这个

函数打印正确的结果但返回无:


def add(x,y):

如果x == 0:

打印y

返回y

else :

x - = 1
$ b $ + = 1

add(x,y)


打印添加(2,4)


结果:

6

没有


马丁

There is problaly a really simple answer to this, but why does this
function print the correct result but return "None":

def add(x, y):
if x == 0:
print y
return y
else:
x -= 1
y += 1
add(x, y)

print add(2, 4)

result:
6
None

Martin

推荐答案

您想要的任何改变:


" return add( X,Y)"在其他地方?
ma ************ @ gmail。 com 写道:
any change you want to have :

"return add(x,y)" in the else ?
ma************@gmail.com wrote:
这可能是一个非常简单的答案,但为什么这个
函数打印正确的结果但返回无:

def add(x,y):
如果x == 0:
打印y
返回y
否则:
x - = 1 y + = 1
添加(x,y)

打印添加(2,4)

结果:
6


Martin
There is problaly a really simple answer to this, but why does this
function print the correct result but return "None":

def add(x, y):
if x == 0:
print y
return y
else:
x -= 1
y += 1
add(x, y)

print add(2, 4)

result:
6
None

Martin






您想要的任何改变:


return add(x,y)在其他地方?
ma ************ @ gmail。 com 写道:
any change you want to have :

"return add(x,y)" in the else ?
ma************@gmail.com wrote:
这可能是一个非常简单的答案,但为什么这个
函数打印正确的结果但返回无:

def add(x,y):
如果x == 0:
打印y
返回y
否则:
x - = 1 y + = 1
添加(x,y)

打印添加(2,4)

结果:
6


Martin
There is problaly a really simple answer to this, but why does this
function print the correct result but return "None":

def add(x, y):
if x == 0:
print y
return y
else:
x -= 1
y += 1
add(x, y)

print add(2, 4)

result:
6
None

Martin






2005年11月18日05:22:47 -0800, ma ************ @ gmail.com

< ;毫安************ @ gmail.com>写道:
On 18 Nov 2005 05:22:47 -0800, ma************@gmail.com
<ma************@gmail.com> wrote:
有一个非常简单的答案,但为什么这个
函数打印正确的结果但返回无:

def add (x,y):
如果x == 0:
打印y
返回y
否则:
x - = 1
y + = 1
add(x,y)

print add(2,4)

结果:
6
There is problaly a really simple answer to this, but why does this
function print the correct result but return "None":

def add(x, y):
if x == 0:
print y
return y
else:
x -= 1
y += 1
add(x, y)

print add(2, 4)

result:
6
None




每个函数都返回一个值。如果您没有使用明确的返回

关键字,则会隐式返回None。你从你的函数中打印出你想要的答案,然后从该函数打印返回值

,正如我已经解释的那样,这将是没有。


-

干杯,

Simon B,
si *** @ brunningonline.net
http://www.brunningonline.net/simon/blog/


这篇关于通过递归添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆