How to print the student record data (a list of values) from a list of records nicely formatted?

Question:

I need help with my homework.

The requirements are:

  1. The source code shall be written by Python3

  2. The code shall contain the following:

    if-else statement

    if-elif-else statement

    while statement

    for statement

    list

My code:

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)

The output when displaying the list looks like:

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

But the output needs to look like this:

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

And continues until it prints the entire student records.

How do I print it like that?

Asked By: Wink101

||

Answers:

Try this!

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

You can use f formatted string or .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]))
Answered By: Aman Raparia

Read up on string formating – python3.6 onwards supports f-strings – before that you can use str.format():

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

before python 3.6 str.format() is used to format strings, you can solve your output f.e. like this:

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]}")

Output:

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:

Answered By: Patrick Artner

Your code needs to be modified like this: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]))
Answered By: I_Al-thamary

Stud_list you created is a list of list where you have stored all your data.
you can use multiple for loops or the index of the sublist.
You can use below code to print the desired output.

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()
Answered By: Akshay Sapra
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.