2015-01-06 27 views
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()方法将项目添加到通过列表,并应返回列表,直到“停止”在列表中。如果 “停” 在列表中,然后返回列表更新python列表上的转移呼叫返回无

print lst打印['bbb', 'dddd', 'bbb', 'stop']#1

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

为什么#2是印刷None,而不是更新的列表?

+0

或者例如http://stackoverflow.com/q/2599149/3001761或http://stackoverflow.com/q/15210646/3001761 – jonrsharpe

回答

2

poi(name, lst) 

应该

return poi(name, lst) 

没有return语句,函数返回隐None