简体   繁体   中英

Using a loop to create multiple lists with user inputs

I'm trying to create 13 different lists (student1 to student13), all with the same structure. Each list has to be filled with the following inputs.

student = []

name = input('Input the name of the student: ')

password = input('Input the 8 digit password: ')
while len(password) != 8:

    print('Password is not 8 digits long!')
    password = input('Input the 8 digit password: ')

grade1 = float(input('Input the first grade between 0 and 10: '))
while grade1 < 0 or grade1 > 10:

    print('Invalid grade!')
    grade1 = float(input('Input the first grade between 0 and 10: '))

grade2 = float(input('Input the second grade between 0 and 10: '))
while grade2 < 0 or grade2 > 10:

    print('Invalid grade!')
    grade2 = float(input('Insert the second grade between 0 and 10: '))

average = (grade1 + grade2) / 2

student.append(name)
student.append(password)
student.append(average)

Is there a way to create a loop to fill 13 different lists with these inputs, or do I have to manually create the inputs for each of the lists?

Not sure if you really want to use input() for that task, but here you go:
students is a list of lists with 13 elements, one for each student.

students = []

for i in range(13):
    student = []
    print(f"\nYou are working on student {i+1}")

    name = input('Input the name of the student: ')

    password = input('Input the 8 digit password: ')
    while len(password) != 8:

        print('Password is not 8 digits long!')
        password = input('Input the 8 digit password: ')

    grade1 = float(input('Input the first grade between 0 and 10: '))
    while grade1 < 0 or grade1 > 10:

        print('Invalid grade!')
        grade1 = float(input('Input the first grade between 0 and 10: '))

    grade2 = float(input('Input the second grade between 0 and 10: '))
    while grade2 < 0 or grade2 > 10:

        print('Invalid grade!')
        grade2 = float(input('Insert the second grade between 0 and 10: '))

    average = (grade1 + grade2) / 2

    student.append(name)
    student.append(password)
    student.append(average)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM