带While循环的成绩计算器

时间:2019-06-22 11:38:02

标签: python

我需要创建一个成绩计算器,对5个测试的结果进行更严格的评估,并对它们进行评估,并给出5个成绩的结果,我制定了最终成绩结果程序,但是我不知道该如何做该循环仅运行五次,并对每个输入的结果求和。

grade = 0
total = 0
for grade in range(0,5):

 if grade >= 0 and grade <= 100:
  grade = grade + int(input('What was your score: '))

 elif grade >= 101: 

  grade += int(input('It should be a number from 0 to 100, what was your 
score: '))

if 93<=grade <= 100:
   print 'A'
elif 90 <= grade < 93:
  print 'A-'
elif 87 <= grade < 90:
  print 'B+'
elif 83 <= grade < 87:
  print 'B'
elif 80 <= grade < 83:
  print 'B-'
elif 77 <= grade < 80:
  print 'C+'
elif 73 <= grade < 77:
  print 'C'
elif 70 <= grade < 73:
  print 'C-'
elif 67 <= grade < 60:
  print 'D+'
elif 63 <= grade < 67:
  print 'D'
elif 60 <= grade < 63:
  print 'D-'
elif grade < 60:
  print 'F'

1 个答案:

答案 0 :(得分:2)

您没有从用户输入中正确收集等级,因为您还将等级值添加了增量值(0-5),这将导致错误的结果。

对于循环,您可以做的是保持对正确输入的成绩数的计数并运行直到达到您的情况下的极限5。

以下是您的代码的可能解决方法:

total = 0
gradeCount = 0

while gradeCount < 5:
    grade = int(input('What was your score: '))

    if grade < 0 or grade > 100:
        print('It should be a number from 0 to 100')
    else:
        gradeCount += 1
        total += grade

        if 93 <= grade <= 100:
           print('A')
        elif 90 <= grade < 93:
            print('A-')
        elif 87 <= grade < 90:
            print('B+')
        elif 83 <= grade < 87:
            print('B')
        elif 80 <= grade < 83:
            print('B-')
        elif 77 <= grade < 80:
            print('C+')
        elif 73 <= grade < 77:
            print('C')
        elif 70 <= grade < 73:
            print('C-')
        elif 67 <= grade < 60:
            print('D+')
        elif 63 <= grade < 67:
            print('D')
        elif 60 <= grade < 63:
            print('D-')
        elif grade < 60:
            print('F')

average = total / gradeCount

print('Average: ' + str(average))