如何从格式良好的记录列表中打印学生记录数据(值列表)?

时间:2019-01-01 13:45:47

标签: python python-3.x

我的作业需要帮助。

要求是:

  
      
  1. 源代码应由Python3编写

  2.   
  3. 代码应包含以下内容:

         

    if-else声明

         

    if-elif-else声明

         

    while声明

         

    for声明

         

    list

  4.   

我的代码:

print("                 Main Menu                 ")
print("[1] Input   Student Records")
print("[2] Display Student Records")
main_choice=int(input("Choice: "))
Stud_list=[]
choice1='y'
if main_choice==1:
    while choice1=='y' or choice1=='Y':
        Stud_number=int(input("Student Number: "))

        Stud_Course=input("Student Course: ")

        Year_Level=int(input("Year Level: "))

        Stud_Name=input("Student Name:")

        Address=input("Address: ")

        Birthday=int(input("Birthdate: "))

        Father_Name=input("Father's Name: ")

        Mother_Name=input("Mother's Name: ")

   Stud_list.append([Stud_number, Stud_Course, Year_Level, Stud_Name, 
                     Address, Birthday, Father_Name, Mother_Name])

         choice1=input("Input another? [Y]/[N]: ")

for i in Stud_list: 
    print(i)

显示列表时的输出如下:

[123456, 'Course', 1, 'Name', 'Here', Birth, 'HIM', 'HER']
[222222, 'Course', 2, 'Name2', 'Here2', Birth, 'HIM', 'HER']

但是输出需要看起来像这样:

Student Number: 123456
Student Course: Course
Year Level: 1
Student Name: Name
Address: Here
Birthdate: Birth
Father's Name: HIM
Mother's Name: HER

Student Number: 222222
Student Course: Course
Year Level: 2
Student Name: Name2
Address: Here2
Birthdate: Birth
Father's Name: HIM
Mother's Name: HER

并继续直到打印出整个学生记录。

我该如何打印?

5 个答案:

答案 0 :(得分:2)

尝试一下!

print('''Student Number: {}\
        \nStudent Course: {}\
        \nYear Level: {}\
        \nStudent Name: {}\
        \nAddress: {}\
        \nBirthdate: {}\
        \nFather\'s Name: {}\
        \nMother\'s Name: {}'''.format(*i))

答案 1 :(得分:2)

您可以使用f格式的字符串或.format

l = [123456, 'Course', 1, 'Name', 'Here', Birth, 'HIM', 'HER']
print("""Student Number: {}\n
       Student Course: {}\n
       Year Level: {}\n
       Student Name: {}\n
       Address: {}\n
       Birthdate: {}\n
       Father's Name: {}\n
       Mother's Name: {}\n""".format(l[0], l[1], l[2], l[3], l[4], l[5], l[6], l[7]))

答案 2 :(得分:2)

string formating上阅读-python3.6及更高版本支持f-strings-在您可以使用str.format()之前:

var1 = "22"
var2 = "2*11"
print(f"{var1} = {var2}")

在使用ypthon 3.6 str.format()格式化字符串之前,可以解决输出问题。像这样:

Stud_list = [[123456, 'Course', 1, 'Name', 'Here', "Birth", 'HIM', 'HER'],
             [222222, 'Course', 2, 'Name2', 'Here2', "Birth", 'HIM', 'HER']]

for stud in Stud_list: 
    print(f"Student Number: {stud[0]}") 
    print(f"Student Course: {stud[1]}")
    print(f"Year Level: {stud[2]}")
    print(f"Student Name: {stud[3]}")
    print(f"Address: {stud[4]}")
    print(f"Birthdate: {stud[5]}")
    print(f"Father's Name: {stud[6]}")
    print(f"Mother's Name: {stud[7]}")

输出:

Student Number: 123456
Student Course: Course
Year Level: 1
Student Name: Name
Address: Here
Birthdate: Birth
Father's Name: HIM
Mother's Name: HER
Student Number: 222222
Student Course: Course
Year Level: 2
Student Name: Name2
Address: Here2
Birthdate: Birth
Father's Name: HIM
Mother's Name: HER

Doku:

答案 3 :(得分:2)

您的代码需要像这样修改:https://onlinegdb.com/BkLmYxFZN

print("                 Main Menu                 ")
print("[1] Input   Student Records")
print("[2] Display Student Records")
main_choice=int(input("Choice: "))
Stud_list=[]
k=[]
choice1='y'
if main_choice==1:
    while choice1=='y' or choice1=='Y':
        Stud_number=int(input("Student Number: "))

        Stud_Course=input("Student Course: ")

        Year_Level=int(input("Year Level: "))

        Stud_Name=input("Student Name:")

        Address=input("Address: ")

        Birthday=int(input("Birthdate: "))

        Father_Name=input("Father's Name: ")

        Mother_Name=input("Mother's Name: ")

        Stud_list.append([Stud_number,Stud_Course,Year_Level,Stud_Name,Address,Birthday,Father_Name,Mother_Name])

        choice1=input("Input another? [Y]/[N]: ")
if main_choice==2:
    if not Stud_list:
        print("List is empty")
    else:
        for i in  Stud_list : 
            print("""
            Student Number: {}
               Student Course: {}
               Year Level: {}
               Student Name: {}
               Address: {}
               Birthdate: {}
               Father's Name: {}
               Mother's Name: {}""".format(i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7]))

答案 4 :(得分:0)

创建的Stud_list是存储所有数据的列表列表。 您可以使用多个for循环或子列表的索引。 您可以使用下面的代码来打印所需的输出。

for i in Stud_list: 
    print("Student Number:",i[0])
    print("Student Course:", i [1])
    print("Year Level:",i[2])
    print("Student Name:",i[3])
    print("Address: ",i[4])
    print("Birthdate: ",i[5])
    print("Father's Name: ",i[6])
    print("Mother's Name: ",i[7])
    print()