简体   繁体   English

没有使用递归正确附加并且无法找到列表长度

[英]Not appending properly using recursion and unable to find list length

In an exercise I need to simulate a sleepwalker (kind of), and one of the variables it needs to return are the amount of steps taken before the function stops.在一个练习中,我需要模拟一个梦游者(一种),它需要返回的变量之一是在 function 停止之前采取的步数。 I'm trying to accomplish this by wanting to append the type of movement to a list "steps", and returning the length of that list when done.我试图通过将 append 的移动类型转换为列表“步骤”并在完成后返回该列表的长度来完成此操作。 But it's not appending properly, the list is filled with 'None' and the length stays 0, why is that and how can I fix it?但它没有正确附加,列表中填充了“无”并且长度保持为 0,为什么会这样,我该如何解决? (I am not allowed to use for/while loops) (我不允许使用 for/while 循环)

I'm talking about the code in the function rwsteps, though I've provided everything since it uses recursion我说的是 function rwsteps 中的代码,尽管我已经提供了所有内容,因为它使用了递归

def rs():
"""rs chooses a random step and returns it.
   note that a call to rs() requires parentheses
   arguments: none at all!
"""
return choice([-1, 1])

def rwpos(start, nsteps):
""" Arguments:
    start: Integer that shows the start position of the sleepwalker
    nsteps: A non-negative integer of the amount of random steps from the start position
    Function returns the random-walker's position
    """
#sys.setrecursionlimit(1000)

random_step = rs()

if nsteps == 0:
    return start #position after nsteps

if random_step == 1:
    return rwpos(start + 1, nsteps - 1)
elif random_step == -1:
    return rwpos(start - 1, nsteps - 1)

def rwsteps(start, low, hi):
""" Arguments:
    start: Integer representing the start position of the sleepwalker
    low: Non-negative integer representing the lowest value the sleepwalker is allowed to walk 
    to
    hi: Non-negative integer representing the highest value the sleepwalker is allowed to walk 
    to
    So; hi >= start >= low
    Function returns every step of the sleepwalker and stops if the sleepwalker goes out of 
    bounds (higher than hi or lower than low)
    rwsteps returns the amount of steps it takes the sleepwalker to go out of bounds
    """
#time.sleep(0.1)
sys.setrecursionlimit(1000)

random_step = rs()
steps = [] #type of steps taken

if start < low or start > hi:
    return len(steps) #amount of steps taken

if random_step == 1:
    print("|" + "_"*(start-low) + "S" + "_"*(hi-start) + "|")
    return rwsteps(start + 1, low, hi), steps.append("forwards")

if random_step == -1:
    print("|" + "_"*(start-low) + "S" + "_"*(hi-start) + "|")
    return rwsteps(start - 1, low, hi), steps.append("backwards")

You return steps.append("backwards")" , which is None because the append -function appends an element inplace and does not return anything. Try您返回steps.append("backwards")" ,这是None因为append -函数在原地附加一个元素并且不返回任何内容。试试

return rwsteps(start - 1, low, hi), steps + ["backwards"]

instead.反而。 This should at least fix the issue with the list full of None s.这至少应该解决列表中充满None的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM