返回函数中的列表。另外,while循环问题

时间:2012-11-17 23:44:31

标签: python list function while-loop

我在使用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()  

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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即可。 python的一个很好的功能是检查response是否不只是n,而是n附近的任何内容使用:

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

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

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

最后,您还可以在退回后打印列表。查看已修改的def main():