What is this "studentrecord"?

Viewed 45

I based my code on Duncan Betts' codes for my assignment but I don't understand it and it seems like it has been 3 years since he last logged on so I can't ask him.

Can you please explain where did the "studentrecord" code come from? What is it?

num = int(input("How many students?: "))

physics_students = [[input("Input student name: "),float(input("Input grade: "))] for studentrecord in range(num)]
physics_students.sort(key=lambda studentrecord: float(studentrecord[1]))


lowest_grade = physics_students[0][1]

ind = 0
while physics_students[ind][1] == lowest_grade:
    ind += 1
    
second_lowest_grade = physics_students[ind][1]
second_lowest_students = []


while physics_students[ind][1] == second_lowest_grade:
    second_lowest_students.append(physics_students[ind][0])
    ind += 1
    if ind == num:
        break


second_lowest_students.sort()


print(*second_lowest_students, sep="\n")

Thank you so much for your help!

1 Answers

The two occurrences of studentrecord refer to 2 different things

In the list comprehension, studentrecord is used to hold each element of the range range(num). It's basically an index, but it's never used anyways.

Edit: I don't think the list comprehension should call it studentrecord because the elements of that range are indices and not lists representing a student's name and grade. It's a little confusing, and the variable should probably be renamed to something like i or _.

That list comprehension is like doing this:

physics_students = []
for studentrecord in range(num):
  physics_students.append([input("Input student name: "),float(input("Input grade: "))])

or this:

physics_students = []
for studentrecord in range(num):
  physics_students[studentrecord] = [input("Input student name: "),float(input("Input grade: "))]

In the lambda expression, studentrecord is the name of the parameter to your anonymous function. It's like saying this:

def my_lambda(studentrecord):
  return float(studentrecord[1]
physics_students.sort(key=my_lambda)
Related