我不知道这是否完全与递归有关,但我在main函数中添加了一个小函数,用
def main(depth, size):
depth = eval(input("Enter depth that is an integer: "))
if (isinstance( depth, int)) == False:
print( 'Not an integer' )
main(depth, size)
else:
pass
turtle.left(120)
turtle.speed(100)
triangle(depth, size)
def triangle(depth, size):
if depth == 0:
pass
else:
turtle.forward(size)
triangle(depth-1, size/2)
turtle.right(120)
turtle.forward(size)
turtle.left(120)
triangle(depth-1, size/2)
turtle.right(120)
turtle.right(120)
turtle.forward(size)
turtle.right(120)
main(depth, 100)
当我输入一个整数时,程序运行正常,当我输入一个非整数时,它返回并告诉我它不是整数,然后返回到输入阶段。然后当我输入一个整数时,它开始按它应该的方式绘制图片,然后再进一步,在第27行的递归处挂起“三角形(depth-1,size/2)”。你知道吗
我马上就要完成这个项目了,我只需要让它更难崩溃。你知道吗
用户输入不适合(非尾部)递归,因为深度是无限的。只需迭代:
相关问题 更多 >
编程相关推荐