验证列表的长度,并在必要时添加输入

时间:2019-06-18 10:16:21

标签: python list

我试图用用户输入创建一个列表,该列表中至少包含八个项目。我可以列出该列表并放入用户输入中,但是我需要确认确实有八个项目,如果没有则要求更多。然后我需要打印列表。

我尝试对len(list)<8使用while语句,对于相同的if / else语句。两者都要求额外的输入,但是都没有在最后打印列表。我尝试了一个使用len(list)<8的嵌套循环,而里面是一个if / else循环,但是返回的错误与原始的while语句相同。

>>>def main():
...     userinput= input("Enter a list of at least eight words separated by a comma: ")
...     list= userinput.split(",")
...     while len(list)<7:
...             print("Please enter more words")
...             more_input= input()
...             more_input.split(",")
...             list.append(more_input)
...     print(list)

OR

>>> def main():
...     userinput= input("Enter a list of at least eight words separated by a comma: ")
...     list= userinput.split(",")
...     if len(list)<7:
...             print("Please enter more words")
...             more_input= input()
...             more_input.split(",")
...             list.append(more_input)
...     else:
...             print(list)

while循环出错:即使列表具有最少的必需输入,它也会不断请求更多输入

>>> main()
Enter a list of at least eight words separated by a comma: This, is, a, list
Please enter more words
More, words
Please enter more words
Three, more, words
Please enter more words

if / else循环出错:仅检查一次。如果长度合适,它将打印列表。如果长度不好,则要求更多输入,然后停止。它既不会再次检查长度,也不会打印列表。

3 个答案:

答案 0 :(得分:0)

如果要将合并的子列表合并到主列表中,请尝试以下操作:

def main():
    list_= []
    print("Enter a list of at least eight words separated by a comma: ")
    while len(list_)<7:
        print("Please enter more words")
        userinput = input()
        temp = userinput.split(",")
        list_ += temp
    print(list_)
main()

输出

Enter a list of at least eight words separated by a comma: 
Please enter more words
This, is, a, list
Please enter more words
more, words
Please enter more words
three, more, words
['This', ' is', ' a', ' list', 'more', ' words', 'three', ' more', ' words']

注意:避免将变量名称分配为list,因为它是python中的内置关键字。

答案 1 :(得分:0)

您的代码似乎还可以,但是问题是您正在拆分来自用户的输入,但是此拆分输入没有变量。我的意思是,您仍在将非拆分输入添加到列表中。我编辑了您可以在下面看到的代码。

def main():
     userinput= input("Enter a list of at least eight words separated by a comma: ")
     input_list = userinput.split(",")
     while len(input_list)<7:
             print("Please enter more words")
             more_input= input()
             splitted_more_input = more_input.split(",") # problem fixed here
             for i in splitted_more_input: # split creates another list 
                 input_list.append(i) # add inputs individual
    print(input_list)

答案 2 :(得分:0)

由于您需要重复执行某个函数直到满足特定条件,因此可以按照以下方式使用递归函数

def main():
    userinput= input("Enter a list of at least eight words separated by a comma: ")
    words = userinput.split(",")

    if len(words) == 8:
        print (words)        
    else:
        A = reenter_words(words)
        print (A) 

def reenter_words(words):
    if len(words) == 8:
        return words
    else:
        IN = input("More words are needed:")
        new_words = words + IN.split(",")
        return reenter_words(new_words)

在这里,我递归地调用reenter_words函数,直到从用户那里得到八个单词为止。

样品输出

Enter a list of at least eight words separated by a comma: qq,ww,ee,rr,tt
More words are needed:gg,hh
More words are needed:kk
['qq', 'ww', 'ee', 'rr', 'tt', 'gg', 'hh', 'kk']

希望这会有所帮助!