我是Python的新手,我正在上一门教我语言的课程。现在,我们刚刚学习了条件语句,我的任务是编写基于三个加权类别(在代码中显示)计算学生GPA的代码。但是,我只能根据某些百分比打印出GPA。 A 4.0是94及以上,3.5是89,3.0是80,2.5是75,2.0是70,1.5是65,1.0是60,60以下任何东西都是0.我的老师说我必须使用这些数字。如果百分比不符合这些条件,我不知道如何计算GPA。有人可以帮助解决问题吗?
#Scores (made up point totals)
avgHwScore = float(input('What was your average homework score? (Out of 5)\n>>> '))
midTerm = float(input('What was your score on the mid-term? (Out of 30)\n>>> '))
finalExam = float(input('What was your score on the final exam? (Out of 50)\n>>> '))
#Converting to decimal
hwDec = avgHwScore/5
midDec = midTerm/30
finalDec = finalExam/50
#Weighted grades
hwWeighted = hwDec*.6
midWeighted = midDec*.2
finalWeighted = finalDec*.2
print('\nThe homework is weighted 60% and the mid-term and final exam are both weighted 20% each\n')
print('You earned ' + str(hwWeighted*100) + '% out of 60% in the homework category')
print('You earned ' + str(midWeighted*100) + '% out of 20% on mid-term category')
print('You earned ' + str(finalWeighted*100) + '% out of 20% on the final exam category\n')
#Final grade
finalGrade = (hwWeighted + midWeighted + finalWeighted)*100
if finalGrade >= 94:
print('Your final grade is ' + str(finalGrade) + '%. You have a 4.0 in the class')
elif finalGrade == 89:
print('Your final grade is ' + str(finalGrade) + '%. You have a 3.5 in the class')
elif finalGrade == 80:
print('Your final grade is ' + str(finalGrade) + '%. You have a 3.0 in the class')
elif finalGrade == 75:
print('Your final grade is ' + str(finalGrade) + '%. You have a 2.5 in the class')
elif finalGrade == 70:
print('Your final grade is ' + str(finalGrade) + '%. You have a 2.0 in the class')
elif finalGrade == 65:
print('Your final grade is ' + str(finalGrade) + '%. You have a 1.5 in the class')
elif finalGrade == 60:
print('Your final grade is ' + str(finalGrade) + '%. You have a 1.0 in the class')
elif finalGrade < 60:
print('Your final grade is ' + str(finalGrade) + '%. You have a 0.0 in the class')
答案 0 :(得分:2)
对于你的代码,假设你的GPA只能是0.5的倍数,Python允许使用一个范围内的条件,我的意思是:
if finalGrade >= 94:
print('Your final grade is ' + str(finalGrade) + '%. You have 4.0 GPA')
elif finalGrade >= 89:
print('Your final grade is ' + str(finalGrade) + '%. You have 3.5 GPA')
答案 1 :(得分:0)
如果使用显式条件,只需使用==
替换代码中的>=
即可。但是,我不鼓励你使用显式条件,因为它是编写代码的一种非常天真的方式,并且它不具备可扩展性。
以下设置适用于以下所有技术。
MAP = [(60, 1), (65, 1.5), (70, 2), (75, 2.5), (80, 3), (89, 3.5), (94, 4)]
BREAKPOINTS, GPAS = zip(*MAP)
使用升序的原因是bisect
模块似乎需要它。
def scoretogpa_linear1(score):
for index, breakpoint in enumerate(BREAKPOINTS):
if score < breakpoint:
return GPAS[index - 1] if index else 0
return GPAS[index]
itertools
进行线性搜索:from itertools import dropwhile
def scoretogpa_linear2(score):
index = next(dropwhile(lambda indexed_breakpoint: score >= indexed_breakpoint[1], enumerate(BREAKPOINTS)), (len(BREAKPOINTS), None))[0]
return GPAS[index - 1] if index else 0
这是由官方Python文档中的score to grade示例推动的。
from bisect import bisect_right
def scoretogpa_log(score):
index = bisect_right(BREAKPOINTS, score)
return GPAS[index - 1] if index else 0
SCORERS = scoretogpa_linear1, scoretogpa_linear2, scoretogpa_log
for breakpoint in BREAKPOINTS:
for score in breakpoint - 1, breakpoint, breakpoint + 1:
gpas = [scorer(score) for scorer in SCORERS]
assert len(set(gpas)) == 1
print(f'{score} {gpas[0]}')
结果:
59 0
60 1
61 1
64 1
65 1.5
66 1.5
69 1.5
70 2
71 2
74 2
75 2.5
76 2.5
79 2.5
80 3
81 3
88 3
89 3.5
90 3.5
93 3.5
94 4
95 4
答案 2 :(得分:0)
当您使用Akumzui提出的建议进行更改时,您的代码将正常运行。但是,使用多个if-else if块只会使代码不必要地长。以下是使用dictionary来缩短它的方法。只使用Python 3.6或更高版本,这是安全的,因为字典中键的顺序是保留的。
grade_reference = {
94: 4.0,
89: 3.5,
80: 3.0,
75: 2.5,
70: 2.0,
65: 1.5,
60: 1.0
}
finalGrade = 78
CGPA = 0.0
for item in grade_reference:
if finalGrade >= item:
CGPA = grade_reference[item]
break
print(CGPA) #Prints 2.5
字典与您现在的学习范围相差不远,因为它是Python中的基本概念。当for循环开始时,变量item
接收字典内的第一个左侧值,称为键。如果您评分的等级高于该等级,则会打印相应的CGPA,即所谓的值。如果没有,它会转到下一个键。