所以昨天我完成了我设计的代码,询问学校管理员你想要跟踪的学生数量,他们各自的成绩,最重要的是他们所学的课程。
这样给我一个代码:
COLS= int(input("number of students to enter: "))
ROWS= int(input("number of grades per student: "))
number =[]
for c in range(COLS):
studentinfo=[]
student =(input("enter student Name: "))
studentinfo.append(student)
for r in range (ROWS):
course=input("Enter course Code: ")
studentinfo.append(course)
grades =float(input("Enter grade for module: "))
studentinfo.append(grades)
number.append(studentinfo)
print(number)
这给了我一个示例输出:
number of students to enter: 2
number of grades per student: 1
enter student Name: KenL
Enter course Code: MA344
Enter grade for module: 80
enter student Name: Harry
Enter course Code: PY101
Enter grade for module: 60
[['KenL', 'MA344', 80.0], ['Harry', 'PY101', 60.0]]
现在的想法是当前输出,我想创建一个功能,它将获取学生列表和课程代码并返回一个新列表,其中包含学生列表中等级高于平均成绩的学生姓名在课程代码中。
例如:above_avg(number," MA22")返回MA22中表现优于平均水平的学生姓名列表。
我已经开始编写此代码:
lookup=input("Which course code do you want to lookup: ")
def find_above_avg(number,lookup):
if lk in number:
avg=...
如果有人对我如何更改代码有任何建议,我可以进行查询,我们将不胜感激。
答案 0 :(得分:1)
l = [['KenL', 'MA344', 80.0], ['Harry', 'PY101', 60.0]]
lookup=raw_input("Which course code do you want to lookup: ")
avg = 60
def find_above_avg(number):
return [i for i in l if lookup in i and i[2] > avg]
print find_above_avg(avg)
输出:
Which course code do you want to lookup: MA344
[['KenL', 'MA344', 80.0]]
答案 1 :(得分:0)
好吧,你可以使用列表。
首先,我们可以尝试选择参加演讲的学生:
def select_by_course(list_of_students, course):
return [student for student in list_of_students if student[1] == course]
很容易找到平均值:
def average_grade(list_of_students, course):
grades = [grade for (name, code, grade) in list_of_students if code == course]
return sum(grades) / len(grades)
然后,我们意识到做你想做的事情并不困难:
def find_above_average(list_of_students, course):
relevant_students = select_by_course(list_of_students, course)
average = average_grade(relevant_students, course)
return [student for student in relevant_students if student[2] > average]
在这里,我们在三个循环中完成(一个在整个数据上,两个在相关学生上),如果你愿意,你可以用两个循环(试一试)。