简体   繁体   English

使用循环创建具有用户输入的多个列表

[英]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.我正在尝试创建 13 个不同的列表(student1 到 student13),它们都具有相同的结构。 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?有没有办法创建一个循环来用这些输入填充 13 个不同的列表,还是我必须手动为每个列表创建输入?

Not sure if you really want to use input() for that task, but here you go:不确定您是否真的想为该任务使用input() ,但在这里您是 go:
students is a list of lists with 13 elements, one for each student. students是一个包含 13 个元素的列表,每个学生一个。

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)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用循环存储多个用户输入 - Storing Multiple User inputs while using a loop 如何在使用循环时创建列表用户输入? - How can I create a list user inputs while using a loop? 如何在python中使用for循环从用户那里获取多个输入? - how to take multiple inputs from user using for loop in python? 如何使用for循环从用户那里获取多个输入? - How to take multiple inputs from user using for loop? Python:运行具有多个输入的for循环并将结果排序到不同的列表中 - Python: Running a for loop with multiple inputs and sorting the results into different lists 在多个列表上使用带有xrange()的for循环 - using a for loop with xrange() on a multiple lists 将多个输入放入列表 - taking multiple inputs into lists 使用while循环计算用户输入的数量 - Using while loop to count the number of user inputs 尝试使用 While 循环创建列表列表,并附加一个使用输入清除和更新的列表。 不工作。 怎么修? - Trying to create a list of lists by using a While Loop and appending a list that clears and updates with inputs. Doesn't work. How to fix? 编写一个程序,使用循环从用户那里获取 3 个键值输入,并使用这些键和值创建一个字典 - Write a program that uses a loop to take 3 key-value inputs from the user and create a dictionary using these keys and values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM