Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I loop a question until the list is complete

enter code hereHow can I loop a question to fill up a list? I need user to input five numbers. Everytime user inputs a number I append the number into a list. My problem is, the code doesn’t loop, so it only takes one input from user and the code stops.

here’s the extract of my code:

    def funct1():
        for i in range(5):
            user = int(input('Enter a Number: '))
            userList.append(user)
            return userList
        
    userList = []   
    Sum_Num()
    print(userList)
        

I tried doing

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for l in range(5) and while True but none worked.

>Solution :

Try this:

def funct1():
    userList = []
    while len(userList) < 5:  # Keep looping until the list has 5 numbers
        user = int(input('Enter a Number: '))
        userList.append(user)

    return userList

userList = funct1()
print(userList)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading