簡體   English   中英

使用循環創建具有用戶輸入的多個列表

[英]Using a loop to create multiple lists with user inputs

我正在嘗試創建 13 個不同的列表(student1 到 student13),它們都具有相同的結構。 每個列表都必須填寫以下輸入。

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)

有沒有辦法創建一個循環來用這些輸入填充 13 個不同的列表,還是我必須手動為每個列表創建輸入?

不確定您是否真的想為該任務使用input() ,但在這里您是 go:
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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM