Validate length of list and adding input if necessary

Question:

I am trying to create a list with user input that has at least eight items in the list. I can make the list and put in the user input, but I need to validate that there are indeed eight items, and ask for more if there are not. Then I need the list to print.

I have tried using a while statement for len(list)<8, and an if/else statement for the same. Both are asking for the additional input, but neither are printing the list at the end. I tried a nested loop with while len(list)<8 and inside is an if/else loop, but that returned the same errors as the original while statement.

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

Errors with while loop: It just keeps asking for more input even when the list has the minimum required input

>>> 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

Errors with if/else loop: It only checks once. If the length is good, it prints the list. If the length is not good, it asks for more input and then stops. It neither checks the length again nor prints the list.

Asked By: Gloria.S

||

Answers:

Try this if you want to merge the split sub-lists in the main list :

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

Output :

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']

Note : Avoid assigning variable name as list as it’s builtin keyword in python.

Your code seems ok but the problem is that you are splitting the input coming from user but this split input does not have a variable. I mean, you are still adding the non split input to the list. I edited the code which you can see below.

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)
Answered By: Mustafa Burak Cin

Since you need to repeatedly execute a function till a certain condition is met, you could take the help of recursive functions as follows

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)

Here I am recursively calling the reenter_words function till we get eight words from the user.

SAMPLE OUTPUT

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']
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.