[英]Python recursion, printing values between two integers
I am wanting to recursively print all integers between two integers given as inputs to the print_numbers
function. 我想递归地打印两个整数之间的所有整数,这些整数作为print_numbers
函数的输入提供。
So far I have gotten: 到目前为止,我已经:
def print_numbers(start,stop):
if start == stop:
print(start)
else:
print(start)
print(print_numbers(start + 1,stop))
But when calling print_numbers(1,5)
I get: 但是当调用print_numbers(1,5)
我得到:
1
2
3
4
5
None
None
None
None
I can't seem to figure out where the none is coming from. 我似乎无法弄清楚它们的来源。
When you are calling print_between
, print_between
does not return anything and so when you do print(print_between(start + 1,stop))
, it prints None
because the function returns None
. 当您调用print_between
, print_between
不返回任何内容,因此当您执行print(print_between(start + 1,stop))
,它会打印None
因为该函数返回None
。
Replace 更换
print(print_between(start + 1,stop))
with 与
print_between(start + 1,stop)
Also, make sure your function name is print_between
and not print_numbers
另外,请确保您的函数名称是print_between
而不是print_numbers
The default return value of a function is None. 函数的默认返回值为None。 you don't need to print it. 您不需要打印它。
def print_numbers(start,stop):
if start == stop:
print(start)
else:
print(start)
print_between(start + 1,stop)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.