使用while循环的平均成绩

时间:2016-10-05 20:18:54

标签: python python-3.x while-loop

我正在尝试使用while循环平均成绩。以下是计划要求:

让用户输入平均成绩。用户可以输入任意数量的等级;当他或她输入“退出”或“退出”打印平均值时,四舍五入到小数点后两位。

请保持建议相对简单。第一次编程。我现在只熟悉for循环和while循环以及if else语句。

我得到了这个骨架代码:

def main():
    '''Average grades entered by the user'''
    #Set the total to 0
    #Set the grade count to 0
    #Get a grade
    #While the grade is not "quit" or "Quit"
    #    Add the grade to total
    #    Add one to the grade count
    #    Get a grade
    #Average the grades
    #Round the average to two decimal places
    #Print the average

main()  #Start execution

到目前为止,这是我的代码:

def main():
    '''Average grades entered by the user'''
    total = 0#Set the total to 0
    grade_count = 0#Set the grade count to 0
    grade = int(input('Enter grade :'))#Get a grade
    while grade != 'quit' or grade != 'Quit':#While the grade is not "quit" or "Quit"
        total += grade#    Add the grade to total
        grade_count += 1#    Add one to the grade count
        grade = int(input('Enter grade :'))#    Get a grade
    average = total / grade_count#Average the grades
    round(average, 2)#Round the average to two decimal places
    print(average)#Print the average

main()  #Start execution

这是我得到的错误:

Enter grade :70
Enter grade :80
Enter grade :90
Enter grade :quit
Traceback (most recent call last):
  File "\\trace\Class\CSC-130\004\Rall992\Asn2\Asn2-1.py", line 21, in        <module>
    main()  #Start execution
  File "\\trace\Class\CSC-130\004\Rall992\Asn2\Asn2-1.py", line 16, in main
    grade = int(input('Enter grade :'))#    Get a grade
ValueError: invalid literal for int() with base 10: 'quit'

0 个答案:

没有答案