确定平均成绩和显示字母等级

时间:2013-10-27 04:54:10

标签: python average

我正在制作课程,找到5个输入考试成绩的平均值,然后显示与每个字母得分相关的字母等级。字母得分是10分制(A = 90-100 B = 80-89等)

这是我到目前为止所写的内容,但它似乎没有在语法中识别出“avg”。有什么建议?

def main():
    while true:
        grade = int(input('Enter grade: '))
        total += grade
        avg = calc_average(total)
        abc_grade = determine_grade(grade)

        print('Average grade is: ' avg)
        print('Letter grades for entered grades are: ' abc_grade)

    def calc_average(total):
       return total / 5

    def determine_grade(grade):
        if grade >= 90 and <= 100:
            return 'A'
        elif grade >= 80 and <= 89:
            return 'B'
        elif grade >= 70 and <= 79:
            return 'C'
        elif grade >= 60 and <= 69:
            return 'D'
        else:
            return 'F'

main()

4 个答案:

答案 0 :(得分:3)

使用:

    print('Average grade is: '+str(avg))
    print('Letter grades for entered grades are: '+abc_grade)

    print('Average grade is: %.2f'%(avg))
    print('Letter grades for entered grades are: %s'%(abc_grade))

答案 1 :(得分:1)

_list = []


def calc_average(total):
    return total / 5


def determine_grade(grade):
    if grade >= 90 and grade <= 100:
        return 'A'
    elif grade >= 80 and grade <= 89:
        return 'B'
    elif grade >= 70 and grade <= 79:
        return 'C'
    elif grade >= 60 and grade <= 69:
        return 'D'
    else:
        return 'F'


while True:
    grade = int(input('Enter grade: '))
    _list.append(grade)

    avg = calc_average(sum(_list))
    abc_grade = ' '.join([determine_grade(mark) for mark in _list])

    if len(_list) > 5:
        break

print('Average grade is: ', avg)
print('Letter grades for entered grades are: ', abc_grade)

答案 2 :(得分:0)

这对我有用..除了你的代码之外,一些小的改动工作正常。

def main():
    total = 0;avg = 0;abc_grade = 0

def calc_average(total):
    return total / 5

def determine_grade(grade):
    if 90 <= grade <= 100:
        return 'A'
    elif 80 <= grade <= 89:
        return 'B'
    elif 70 <= grade <= 79:
        return 'C'
    elif 60 <= grade <= 69:
        return 'D'
    else:
        return 'F'

while(True):
    grade = int(input('Enter grade: '))
    total += grade
    avg = calc_average(total)
    abc_grade = determine_grade(grade)

    print('Average grade is: ' + str(avg))
    print('Letter grades for entered grades are: ' + str(abc_grade))


main()  

希望你能找出差异。 :)

答案 3 :(得分:0)

def main():

  print("This is a program which displays the grade from a score")

  print("")

  grade = eval(input("What is the value of the score :  "))

  print("")

  if 90 <= grade <= 100:
    print("Your get an A")

  elif 80 <= grade <= 89:
    print("Your get a B")
  elif 70 <= grade <= 79:
    print("Your get a C")
  elif 60 <= grade <= 69:
    print("Your get a D")
  else:
    print("Your get an F")


main()