python嵌套的列表储存

简单列表循环清楚了,但是嵌套的列表储存会有点蒙圈,记录下

方式一:调用方法时输出print,方法是直接return输出信息

def get_grade(student_score, name):
    for student in student_score:
        if student[1] == name:
            try:
                grade = student[2]
                return grade
      
            except IndexError:
                return '找不到该同学的成绩'
    return "找不到该同学的信息"


student_score = [[1, "Lily", 95], [2, "Bob", 83], [3, "Tom"], [4, "Lis", 89]]

a=get_grade(student_score, "Tom")
print(a)
b=get_grade(student_score, "234")
print(b)
c=get_grade(student_score, "Lily")
print(c)

方式二:方法中写了输出,再return

def get_grade(student_score, name):
    for student in student_score:
        if student[1] == name:
            try:
                grade = student[2]
                print("{}的成绩是:{}".format(name, grade))
                return
            except IndexError:
                print('找不到该同学的成绩')
                return
    print("找不到该同学的信息")
    return


student_score = [[1, "Lily", 95], [2, "Bob", 83], [3, "Tom"], [4, "Lis", 89]]

get_grade(student_score, "Bob")
get_grade(student_score, "Tom")
get_grade(student_score, "234")