2012-11-17 64 views
1

我在創建Python函數時遇到問題。我希望我的函數允許用戶在列表中輸入單詞,並詢問用戶是否要輸入其他單詞。如果用戶輸入'n',我希望函數返回列表中輸入的單詞。當我運行該功能時,會詢問用戶是否要再輸入一個單詞。而且,它不會返回列表。返回函數中的列表。另外,問題while循環

def add_list(x): 
    first_list = raw_input('Please input a word to add to a list ') 
    x.append(first_list) 
    response = None 
    while response != 'n': 
     response = raw_input('Would you like to enter another word? ') 
     if response == 'n': 
      print 'Here is the list of words' 
      return x    
     else: 
      add_list(x) 


def main(): 
    add_list(x) 


x = [] 
main() 
+3

我不會從一個新手的遞歸方法開始。儘量避免從'add_list'中調用'add_list',並且您可能更容易。 –

+1

將'else'語句中的文本更改爲'return add_list(x)'。你想要所有的決策路徑返回列表。 –

+0

除了上面的評論(我也同意)之外,你並沒有在任何地方打印你的列表。 'return x'返回列表,但沒有做任何事情。如果你在'main()'的'add_list(x)'前面加上'print',你應該在第一次運行時按'n'來看你的列表。 – RocketDonkey

回答

1

我修改程序一點點:

def add_list(x): 
    first_list = raw_input('Please input a word to add to a list.') 
    x.append(first_list) 
    response = None 
    while response != 'n': 
     response = raw_input('Would you like to enter another word? ') 
     if response.lower() in ['n','no']: 
      print 'Here is the list of words:', x 
      return x    
     else: 
      x.append(response) 

def main(): 
    returned_list = add_list(x) 
    print "I can also print my returned list: ", returned_list 


x = [] 
main() 

一:有沒有必要遞歸。而只需將response附加到您的清單x。蟒蛇的一個很好的功能是檢查是否response不僅僅是n,但任何接近n使用:

if response.lower() in ['n','no']: 

我還編輯了實際打印清單到您的用戶線!

print 'Here is the list of words:', x 

最後,您還可以在返回後列印您的清單。檢查編輯的def main():

2

如上所述,您的代碼不返回任何內容的原因是因爲您實際上沒有打印結果。此外,即使你已經說'n'是由於遞歸導致它繼續詢問你是否想輸入另一個單詞(也就是說,你以嵌套的方式一遍又一遍地調用函數)在Excel中嵌套IFs,如果有幫助:))。一旦你掌握了基本知識,你可以閱讀更多,但現在我會避免它:)

這是一個基本版本的東西,將做你想要的東西,有希望可以幫助你的評論瞭解到底發生了什麼:

def add_list(): 
    # Since you are starting with an empty list and immediately appending 
    # the response, you can actually cut out the middle man and create 
    # the list with the result of the response all at once (notice how 
    # it is wrapped in brackets - this just says 'make a list from the 
    # result of raw_input' 
    x = [raw_input('Please input a word to add to a list: ')] 

    # This is a slightly more idiomatic way of looping in this case. We 
    # are basically saying 'Keep this going until we break out of it in 
    # subsequent code (you can also say while 1 - same thing). 
    while True: 
     response = raw_input('Would you like to enter another word? ') 

     # Here we lowercase the response and take the first letter - if 
     # it is 'n', we return the value of our list; if not, we continue 
     if response.lower()[0] == 'n': 
      return x 
     else: 
      # This is the same concept as above - since we know that we 
      # want to continue, we append the result of raw_input to x. 
      # This will then continue the while loop, and we will be asked 
      # if we want to enter another word. 
      x.append(raw_input('Please input a word to add to the list: ')) 

def main(): 
    # Here we return the result of the function and save it to my_list 
    my_list = add_list() 

    # Now you can do whatever you want with the list 
    print 'Here is the list of words: ', my_list 

main()