我在尝试找出正在编写的程序的代码时遇到麻烦。我得到的文件格式为:
student_firstname
student_lastname
student_number
assignment_mark
midterm_mark
exam_grade
final_mark
以下是示例:
Marilyn
Malone
136238
88
72
70
Esther
Mulcahy
194563
25
45
91
William
Gray
110031
33
38
62
我正在尝试寻找一种方法来从所有学生中退还最高和最低的final_mark。到目前为止,我所做的是:
infile = open(fileName, "r")
myList = []
name = infile.readline().strip()
passingGrades = 0
failingGrades = 0
avGrade = 0
while name != '':
highestGrade = [0, 'studentName']
lowestGrade = [99, 'studentName']
lastName = infile.readline().strip()
studentNum = infile.readline().strip()
assignGrade = infile.readline().strip()
midGrade = infile.readline().strip()
examGrade = infile.readline().strip()
averageGrade = ((int(assignGrade) * 0.25) + (int(midGrade) * 0.25) +
(int(examGrade) * 0.50))
def lowGrade(x):
if x < lowestGrade[0]:
lowestGrade.pop(0)
lowestGrade.pop(0)
lowestGrade.append(x)
lowestGrade.append(name)
lowestGrade.append(lastName)
return lowestGrade
if averageGrade >= 50 and int(examGrade) >= 50:
#print(name)
passingGrades += 1
avGrade += averageGrade
if averageGrade > highestGrade[0]:
highestGrade.pop(0)
highestGrade.pop(0)
highestGrade.append(averageGrade)
highestGrade.append(name)
highestGrade.append(lastName)
else:
pass
else:
failingGrades += 1
avGrade += averageGrade
lowGrade(averageGrade)
name = infile.readline().strip()
finalAverage = avGrade / (passingGrades + failingGrades)
highFinal = ' '.join(str(x) for x in highestGrade)
lowFinal = ' '.join(str(x) for x in lowestGrade)
使用其中一个文本文件运行程序时,我得到:
Number of passes: 9
Number of fails: 1
Average final grade: 64.55
The Highest Grade: 79.5 Patty Marshall
The Lowest Grade: 79.5 Patty Marshall
非常感谢您的帮助!
答案 0 :(得分:0)
每次找到最低成绩时,您将从列表中删除2个值并加3。这意味着从不删除第一个值。您能检查一下是否适合您吗?
def lowGrade(x):
if x < lowestGrade[0]:
lowestGrade.pop(0)
lowestGrade.pop(0)
lowestGrade.append(x)
lowestGrade.append(' '.join(name, lastName))
return lowestGrade
答案 1 :(得分:0)
每次阅读新学生时,您都将{
event: 'subscribe',
user: <USER_ID>,
subscribe: {
transactionId: <CUSTOMER>,
transactionAffiliation: <AFFILIATION>,
transactionTotal: <PRICE>,
transactionProducts: [
{
id: <CUSTOMER>,
sku: <SUBSCRIPTION>,
name: <PLAN>,
price: <PRICEPER>,
quantity: <QUANTITY>
}
]
}
};
和highestGrade
重置为其初始值(分别为lowestGrade
和0
)。
99
通过初始化循环中外部的值来轻松解决此问题。
while name != '':
# stuff that happens for each 'name'
highestGrade = [0, 'studentName']
lowestGrade = [99, 'studentName']
# ... do stuff ...
# read new name
name = infile.readline().strip()
此外,您的highestGrade = [0, 'studentName']
lowestGrade = [99, 'studentName']
while name != '':
# stuff that happens for each 'name'
方法不必要地复杂且多余。无需从列表中删除项目然后添加新项目,只需创建一个新列表即可。而且实际上,tuple
比list
更合适,尽管两者都可以。
lowGrade
您完全不需要使用返回的值,因此无需通过此方法返回lowestGrade = (99, 'studentName')
def lowGrade(x):
if x < lowestGrade[0]:
lowestGrade = (x, name, lastName)
/ tuple
。