Python 7 Levels, L4, Unit 34, Multidimensional List

Powered By EmbedPress

Exercise 1:

score = [["David Smith",90,88,95],["John Doe",78,99,85],["Peter Pan",83,91,75]]
for i in range(len(score)):
    for j in range(len(score[i])):
        print(score[i][j],end = "\t")
    print()#output a new line

Exercise 2:

stu_num = int(input("Please define number of students:"))
stu_all = []

for i in range(stu_num):
    stu = []
    name = input("Please input the student's name:")
    age = int(input("Please input the student's age:"))
    s = int(input("Please input the student's score:"))
    stu.append(name)
    stu.append(age)
    stu.append(s)
    stu_all.append(stu)

print("Name\tAge\tScore")
for i in range(len(stu_all)):
    for j in range(len(stu_all[i])):
        print(stu_all[i][j],end = "  \t")
    print()#output a new line

Exercise 3:

stu_num = int(input("Please define number of the students:"))
stu_all = []

for i in range(stu_num):
    stu = []
    name = input("Please input student's name:")
    age = int(input("Please input student's age:"))
    s = int(input("Please input student's score:"))
    stu.append(name)
    stu.append(age)
    stu.append(s)
    stu_all.append(stu)

#stu_all = [[name1,age1,s1],[name2,age2,s2],[name3,age3,s3]]
while True:
    num = int(input("Please input what you will look(1.Name 2.Age 3.Score 4.Quit)"))
    if num == 4:
        print("Abort system.")
        break
    elif num == 1 or num == 2 or num == 3:
        for i in range(len(stu_all)):
            print(stu_all[i][num-1])
    else:
        print("Wrong input. Please retry")

Homework:

stu_num = int(input("Please input how many students:"))
stu_all = []
sum_score = []#this is for the sum of the scores

for i in range(stu_num):
    stu = []
    name = input("Please input student's name:")
    age = int(input("Please input student's age:"))
    s = int(input("Please input student's score:"))
    stu.append(name)
    stu.append(age)
    stu.append(s)
    sum_score.append(s)
    stu_all.append(stu)
    
for i in range(len(stu_all)-1):
    for j in range(len(stu_all)-1-i):
        if sum_score[j] < sum_score[j+1]:
            ss = sum_score[j]
            sum_score[j] = sum_score[j+1]
            sum_score[j+1] = ss

            stuu = stu_all[j]
            stu_all[j] = stu_all[j+1]
            stu_all[j+1] = stuu


print("Name\tAge\tScore")
for i in range(len(stu_all)):
    for j in range(len(stu_all[i])):
        print(stu_all[i][j],end = "  \t")
    print()#output a new line

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts