我如何使用while循环将N个用户输入添加到列表中?

问题描述 投票:0回答:2
question = ""
while question not in questionList:
    question = questionList.append(input("Whats your question? "))
    if question in questionList:
        break
    elif question == "none":
        break

我想做的是让用户反复问一个问题,直到输入“无”或直到他们重复自己为止。他们输入的内容应该放入“ questionList”列表中,唯一的问题是我的代码无论如何都不会中断。如何使while循环中断?

python while-loop append
2个回答
1
投票
question = "_" questionList=[] while question != "": question = str(input("Whats your question? ")) if question == None or question in questionList: break questionList.append(question) # returns None

2
投票
主要问题是questionList.append()总是返回None,而您的代码似乎假定它返回了其参数。改写如下:
© www.soinside.com 2019 - 2024. All rights reserved.