我看到了一些关于计算递归迭代次数的帖子,但我无法遵循或它们不适用。对不起,如果它是多余的!感谢您的帮助!我正在尝试使用递归函数来纠正随机游走情况的函数并保持对步数的计数。它还有一个视觉组件,它告诉我实际功能似乎正在工作,但不是计数部分。
def rwSteps (start, low, hi ):
""" returns the number of steps the sleepwalker took in order to finally reach the `lower or upper bound
input: an integer start (starting position of the sleepwalker), an integer low (nonnegative, the smallest value our sleepwalker can wander to), and an integer hi (the highest value the sleepwalker can wander to)
"""
count=0
newcount = count +1
' '*start + '~'
print (' '*start + '~')
if low < start and start < hi:
newstart = start + rs()
newcount = count + 1
return rwSteps(newstart, low, hi)
return newcount
elif start == low:
finalcount = newcount +1
return finalcount
elif start == hi:
finalcount = newcount +1
return finalcount