0
def get_name():
    import random
    lst = ["aa", "bbb", "ccc", "dddd", "eee", "stop"]
    return random.choice(lst)

def poi(name, lst):
    res = get_name()
    lst.append(res)
    if res !="stop":
        poi(name, lst)
    else:
        print lst
        return lst


if __name__ == '__main__':
    print poi("xx", [])

poi() method add items in a passed list and should return list until "stop" is in the list. If "stop" is in the list then returns the list

print lst prints ['bbb', 'dddd', 'bbb', 'stop'] #1

But

`print poi("xx", [])` prints `None`  #2

Why #2 is printing None instead of updated list ?

navyad
  • 3,752
  • 7
  • 47
  • 88

1 Answers1

2

The

poi(name, lst)

should be

return poi(name, lst)

Without the return statement, the function is implicitly returning None.

NPE
  • 486,780
  • 108
  • 951
  • 1,012