Python:如何在成绩计算器中添加“最终成绩”转换器
我大概知道该怎么做,但就是搞不明白。这个程序需要能够获取学生的名字和三次考试的分数,然后计算这三次分数的平均值(百分比)。接下来,你还需要把这个分数(百分比)转换成一个等级。
编辑:我该怎么去掉等级和“%”之间的空格呢?
- 请按“Enter”键开始
- 输入你的名字:Jordan Simpson
- 第一次考试分数:67%
- 第二次考试分数:78%
- 第三次考试分数:89%
- 最终等级:C+
- Jordan Simpson的考试分数是:78.0 %
- 你想重新开始这个程序吗?
评分标准:
input ('Please press "Enter" to begin')
while True:
import math
studentName = str(input('Enter your Name: '))
firstScore = int(float(input('First test score: ').replace('%', '')))
secondScore = int(float(input('Second test score: ').replace('%', '')))
thirdScore = int(float(input('Third test score: ').replace('%', '')))
scoreAvg = (firstScore + secondScore + thirdScore) / 3
def grade():
if scoreAvg >= 93 and <= 100:
return 'A'
if scoreAvg <= 92.9 and >= 89:
return 'A-'
if scoreAvg <= 88.9 and >= 87:
return 'B+'
if scoreAvg <= 86.9 and >= 83:
return 'B'
if scoreAvg <= 82.9 and >= 79:
return 'B-'
if scoreAvg <= 78.9 and >= 77:
return 'C+'
if scoreAvg <= 76.9 and >= 73:
return 'C'
if scoreAvg <= 72.9 and >= 69:
return 'C-'
if scoreAvg <= 68.9 and >= 67:
return 'D+'
if scoreAvg <= 66.9 and >= 60:
return 'D'
return 'F'
print(grade(scoreAvg))
print(studentName, "test score is: ",scoreAvg,'%')
endProgram = input ('Do you want to restart the program?')
if endProgram in ('no', 'No', 'NO', 'false', 'False', 'FALSE'):
break
1 个回答
5
我不太确定你的问题是什么,不过这里有一个更简洁的方法来获取字母成绩。
>>> scores = [93, 89, 87, 83, 79, 77, 73, 69, 67, 60, 0]
>>> grades = ['A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'F']
>>>
>>> def gradeFor(s):
... grade_scores = zip(scores, grades)
... for score, grade in grade_scores:
... if s >= score:
... return grade
>>> gradeFor(87)
B+
>>> gradeFor(89)
A-
>>> gradeFor(88)
B+
>>> gradeFor(67)
D+
>>> gradeFor(72)
C-
>>> gradeFor(40)
F
另外,你还可以这样做:
if endProgram.lower() in ('no', 'false'):