如何在while循环中使用append函数?

时间:2018-05-13 10:59:54

标签: python list loops while-loop append

question = ""
while question not in questionList:
    question = questionList.append(input("Whats your question? "))
    if question in questionList:
        break
    elif question == "none":
        break

我想要做的是让用户反复提出一个问题,直到输入" none"或直到他们重复自己。他们输入的内容应该放入" questionList" list唯一的问题是我的代码无论如何都不会破坏。我怎样才能让while循环中断?

1 个答案:

答案 0 :(得分:1)

主要问题是questionList.append()始终返回None,而您的代码似乎假设它返回其参数。改述如下:

question = input("Whats your question? ")
questionList.append(question)