2013-07-05 13 views
2

对于这个任务,我们应该在Python中创建一个课程,我们将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 
+1

我不能重现此。我进入了'75.3''76.3'和'77.3',我的平均值是'C'。请显示您的示例输入。 –

+0

您是否收到错误或者是否跳过了“if-elif”语句? (这似乎不可能) –

+1

如果平均值介于89.0和90.0之间,该怎么办? – chepner

回答

1

没有运气你if-elif陈述并不涵盖所有可能的值。例如,如果平均值为89.5,则没有任何块会捕获它。解决这个问题的最简单方法是从if-elif声明中删除<=条款,因为它们是不必要的。

if avg >= 90.0: 
    print(name +"'s grade is: \t A") 
elif avg >= 80.0: 
    print(name +"'s grade is: \t B") 
elif avg >= 70.0: 
    print(name +"'s grade is: \t C") 
elif avg >= 60.0: 
    print(name +"'s grade is: \t D") 
else: 
    print(name +"'s grade is: \t E") 
+0

但是<= > =不是必须的,因为它基本上是说它小于/大于或等于数字。 – ChrisD93

+0

不,因为如果第一个“if”失败,那么它已经'已经'小于90.0,所以第一个'elif'语句中的条件是不必要的。对于每个连续的'elif'语句都是一样的(你已经消除了前一个'elif'中的<='case)(请参阅编辑我的答案) – bogatron

0

你错过了一些边界情况下,e.g 89.979.7,像这样的平均数都不会打印任何级别在你的程序。

你需要的东西是这样的:

if avg >= 90.0 and avg <= 100.0: 
    print(name +"'s grade is: \t A") 
elif avg >= 80.0 and avg < 90.0:  #should be <90 not 89.0 
    print(name +"'s grade is: \t B") 
elif avg >= 70.0 and avg < 80.0:  #should be <80 not 79.0 
    print(name +"'s grade is: \t C") 
elif avg >= 60.0 and avg < 70.0:  #should be <70 not 69.0 
    print(name +"'s grade is: \t D") 
elif avg >= 0.0 and avg < 60.0:  #should be <60 not 59.0 
    print(name +"'s grade is: \t E") 

演示:

The student's name is: dfgd 
dfgd's test scores are: 
---------------------------: 
TEST1:   89.9 
TEST2:   89.9 
TEST3:   89.9 
dfgd's average is: 89.90000000000002 
dfgd's grade is:  B 

更新:

这里一个清洁的解决方案将是bisect模块:

import bisect 
lis = [0,60,70,80,90,100] 
grades = list('EDCBA') 
ind = bisect.bisect_right(lis,avg) -1 
print(name +"'s grade is: \t {}".format(grades[ind])) 
+0

因此,我应该用80替换79,89等数字, 90?Cuz我只是把什么说的投标 – ChrisD93

+0

@ThatOneDude是的 –

+0

非常感谢你的工作:) – ChrisD93

3

问题出在您的if s。当平均值在8990,7980等之间时,您没有这种情况。此外,您不需要第一个and以外的任何and,因为每个以前的支票都会确认and正在再次检查的内容。您可以将大部分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()