Python:在递归函数中返回后添加新行
我是 python 新手,面临以下问题..我正在尝试执行递归求和函数,但总和不会在新行上返回
例如, sum(2,2) 应该返回
4
2
sum(2,3) 将return
6
4
2
但我得到 4 2 和 6 4 2 都在同一行。这是我的代码:
def sum(a,b):
if a>0 and b>0:
return str(a*b) + " " + str(sum(a,b-1))
else:
return ""
我尝试将“”更改为“\n”,但它不起作用
I am new to python and faced the following problem.. I am trying to do a recursive sum function but the sum does not get returned on a new line
For example, sum(2,2) should return
4
2
sum(2,3) will return
6
4
2
But I get 4 2 and 6 4 2 all on the same line. This is my code:
def sum(a,b):
if a>0 and b>0:
return str(a*b) + " " + str(sum(a,b-1))
else:
return ""
I have tried use changing " " to "\n" but it doesn't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里工作得很好。
works fine here.
您写道将“”更改为“\n”不起作用,但是您尝试打印结果吗?
You wrote that changing " " to "\n" doesn't work, but did you try printing the result?