【发布时间】:2017-03-30 10:12:59
【问题描述】:
您好,我很难确切地理解什么是递归,这是我正在编写的程序(一个蛇游戏)的要求,我无法找到归类为递归的明确定义。有人能告诉我下面的代码是否就是一个例子吗?
while True:
#recieve input
player_name = input("Enter a name between 0 and 20 characters: ")
#if length is within boundaries the loop is stopped
if len(player_name) > 0 and len(player_name) <= 20: break
#if the name is outwith the boundaries a message is printed and the loop restarts
print("Too long/short, try again.")
【问题讨论】:
-
不,这不是递归;你需要一个调用自身进行递归的函数
-
不,这不是递归的例子。递归需要一个函数调用自己。
-
while循环不是递归。
-
递归只是让一个函数在函数内调用自身,例如
def something(): print('a'); something()。 -
如果你需要写它,那一定是为了做一些家庭作业或其他什么,我敢打赌它附带了某种教学材料——它应该包含对递归的解释。
标签: python validation recursion