我有一个程序可以读取一个文件,其中包含学生名、ID、专业和GPA。你知道吗
例如(文件还有很多内容):
OLIVER
8117411
English
2.09
OLIVIA
6478288
Law
3.11
HARRY
5520946
English
1.88
AMELIA
2440501
French
2.93
我得弄清楚:
我现在只有医学专业的荣誉榜。我不知道如何开始计算数学专业的平均绩点。如有任何帮助,我们将不胜感激。你知道吗
这是我目前拥有的代码:
import students6
file = open("students.txt")
name = "x"
while name != "":
name, studentID, major, gpa = students6.readStudents6(file)
print(name, gpa, major, studentID)
if major == "Medicine" and gpa > "3.5":
print("Med student " + name + " made the honor roll.")
if major == "Math":
下面是要导入的students6.py文件:
def readStudents6(file):
name = file.readline().rstrip()
studentID = file.readline().rstrip()
major = file.readline().rstrip()
gpa = file.readline().rstrip()
return name, studentID, major, gpa
您需要表示数据,当前正在从读取文件返回元组。 将它们存储在一个列表中,创建方法来筛选你的专业学生,以及创建给定学生列表的avgGPA的方法。你知道吗
您可能希望使GPA在读取时浮动:
处理返回的学生数据元组的两个新助手方法:
主程序:
输出:
参见:PyTut: List comprehensions和Built in functions(
float
,sum
)输出:
相关问题 更多 >
编程相关推荐