对于这个作业,我们应该用 Python 创建一个类(class),其中我们输入 3 个分数,并且必须找到学生的平均分和成绩。它有效,但我遇到了错误。当我输入一个整数(例如 73)三次时,它会显示字母等级。但是,当我输入带有小数点的数字(例如 83.7)三次时,它不会显示字母等级,而这正是我们需要的数字。有没有办法让我输入带小数的数字时显示成绩?为什么它只适用于整数?
class grades:
def __init__(self,name,test1,test2,test3,avg):
self.name = name
self.test1 = test1
self.test2 = test2
self.test3 = test3
self.avg = avg
def getName(self):
return self.name
def getTest1(self):
return self.test1
def getTest2(self):
return self.test2
def getTest3(self):
return self.test3
def getAvg(self):
return self.avg
#main:
name = input("Enter the students name: ")
test1 = float(input("Enter the first score: "))
test2 = float(input("Enter the second score: "))
test3 = float(input("Enter the third score: "))
avg = float(test1 + test2 + test3) /3.0
grades1 = grades(name,test1,test2,test3,avg)
grades1.name
grades1.test1
grades1.test2
grades1.test3
grades1.avg
print("The student's name is:" ,grades1.name)
print(grades1.name +"'s test scores are:")
print("---------------------------:")
print("TEST1: \t\t\t",grades1.test1)
print("TEST2: \t\t\t",grades1.test2)
print("TEST3: \t\t\t",grades1.test3)
print(grades1.name +"'s average is: \t",grades1.avg)
##if avg <= 100.0 and avg >= 90.0:
## print(name +"'s grade is: \t A")
##elif avg <= 89.0 and avg >= 80.0:
## print(name +"'s grade is: \t B")
##elif avg <= 79.0 and avg >= 70.0:
## print(name +"'s grade is: \t C")
##elif avg <= 69.0 and avg >= 60.0:
## print(name +"'s grade is: \t D")
##elif avg <= 59.0 and avg >= 0.0:
## print(name +"'s grade is: \t E")
if avg >= 90.0 and avg <= 100.0:
print(name +"'s grade is: \t A")
elif avg >= 80.0 and avg <= 89.0:
print(name +"'s grade is: \t B")
elif avg >= 70.0 and avg <= 79.0:
print(name +"'s grade is: \t C")
elif avg >= 60.0 and avg <= 69.0:
print(name +"'s grade is: \t D")
elif avg >= 0.0 and avg <= 59.0:
print(name +"'s grade is: \t E")
我之所以对一年级的部分进行评论,是因为我两种方法都尝试过,但都没有成功。
这是我尝试过的,但小数点运气不好
Enter the students name: k
Enter the first score: 89.9
Enter the second score: 89.9
Enter the third score: 89.9
The student's name is: k
k's test scores are:
---------------------------:
TEST1: 89.9
TEST2: 89.9
TEST3: 89.9
k's average is: 89.90000000000002
最佳答案
问题出在你的if
上。当平均值介于 89
和 90
、79
和 80
等之间时,您没有任何情况。此外,您不需要第一个之外的任何 和
,因为之前的每次检查都会确认 和
再次检查的内容。您可以将大部分 if
缩短为单个条件。
if avg > 100 or avg < 0:
#check for out of range grades first
print('Grade out of range')
elif avg >= 90.0:
print(name +"'s grade is: \t A")
# if the elif is reached, we already know that the grade is below 90, because
# it would have been handled by the previous if, if it were >=90
elif avg >= 80.0:
print(name +"'s grade is: \t B")
elif avg >= 70.0: # other wise the previous check will have caught it
print(name +"'s grade is: \t C")
elif avg >= 60.0:
print(name +"'s grade is: \t D")
elif avg >= 0.0:
print(name +"'s grade is: \t E") # should this be 'F' instead of 'E'?
因为我是个好人;)
class Grades:
def __init__(self, name, *tests):
self.name = name
self.tests = list(tests)
@property
def avg(self):
return sum(self.tests)/len(self.tests)
@property
def letter_avg(self):
avg = self.avg
if avg > 100 or avg < 0:
raise ValueError('Grade out of range')
elif avg >= 90.0:
return 'A'
elif avg >= 80.0:
return 'B'
elif avg >= 70.0:
return 'B'
elif avg >= 60.0:
return 'D'
elif avg >= 0.0:
return 'F'
def __iter__(self):
return iter(self.tests)
def __getattr__(self, attr):
"""Allows access such as 'grades.test1' """
if attr.startswith('test'):
try:
num = int(attr[4:])-1
return self.tests[num]
except (ValueError, IndexError):
raise AttributeError('Invalid test number')
else:
raise AttributeError(
'Grades object has no attribute {0}'.format(attr))
def main():
name = input("Enter the students name: ")
test1 = float(input("Enter the first score: "))
test2 = float(input("Enter the second score: "))
test3 = float(input("Enter the third score: "))
grades1 = Grades(name, test1, test2, test3)
print("The student's name is:" , grades1.name)
print(grades1.name +"'s test scores are:")
print("---------------------------:")
for index, test in enumerate(grades1):
print('TEST{0}: \t\t\t{1}'.format(index, test))
print("{0}'s average is {1}".format(grades1.name, grades1.avg))
print("{0}'s average is \t {1}".format(grades1.name, grades1.letter_avg))
if __name__ == '__main__':
main()
关于python - 我的程序不会给出成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17496099/