1

我在 Python 中创建函数时遇到问题。我希望我的功能允许用户在列表中输入单词并询问用户是否要输入其他单词。如果用户输入“n”,我希望该函数返回列表中输入的单词。当我运行该函数时,它会询问用户是否要输入两次额外的单词。此外,它不会返回列表。

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()  
4

2 回答 2

2

如上所述,您的代码不返回任何内容的原因是因为您实际上并没有打印结果。此外,即使您已经说过“n”,它仍然继续询问您是否要输入另一个单词的原因是由于递归(即您以嵌套方式一遍又一遍地调用函数的事实(想想Excel中的嵌套IF,如果有帮助的话:))。一旦你掌握了基础知识,你就可以更多地阅读它,但现在我会避免它:)

这是一个基本版本的东西,可以做你想做的事,评论希望能帮助你理解正在发生的事情:

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()
于 2012-11-18T00:42:43.877 回答
0

我稍微修改了你的程序:

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。python 的一个很好的特性是检查是否response不仅仅是n,而是任何接近n使用的东西:

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

我还编辑了一行,将列表实际打印给您的用户!

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

最后,您还可以在退货后打印您的清单。查看已编辑的def main():.

于 2012-11-17T23:58:37.510 回答