您需要分割包含原始标记的字符串,然后使用checkGrade函数来获取该原始标记的分数。试试这个:def checkGrade(raw):
if raw>100:
score = "Error, raw score exceed max score"
elif raw>80:
score = 4.00
elif raw>70:
score = 3.00
elif raw>60:
score = 2.00
elif raw>50:
score = 1.00
elif raw>0:
score = 0
else:
score = "Error, raw score exceed min score"
return score
def calGPA(s):
raw_marks = s.split()
num_subjects = len(raw_marks)
if num_subjects > 10:
print('Limit for number of subjects exceeded')
return
total = 0
for raw in raw_marks:
total += checkGrade(int(raw))
print('GPA: {}'.format(total/num_subjects))
calGPA("77 45 77 55 87")
输出:
^{pr2}$
编辑以反映您的新要求def calGPA(s):
gpas = s.split()
valid_grades = ['A', 'B', 'C', 'D', 'F']
if not all(gpa in valid_grades for gpa in gpas):
print("Error")
p.S.:编辑问题使当前接受的答案无效是不允许并且非常不鼓励。