1

我正在开发一个班级程序,该程序会找到 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

4 回答 4

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))
于 2013-10-27T05:00:53.413 回答
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)
于 2013-10-27T05:02:58.940 回答
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()  

希望你能找出不同之处。:)

于 2013-10-27T05:06:05.437 回答
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()  
于 2017-04-05T01:55:11.970 回答