程序不会继续下一个功能,Python 3

时间:2017-10-29 16:22:20

标签: python

我必须为一个简单的程序创建一个模块化的代码,但我无法通过输入成绩。

程序要求5个等级,然后计算等级的平均值并告诉用户数量和等级。

我尝试制作一个主模块,但在收到错误后我把它拿出来仍然出错:

grade1 = int(input('Please enter a grade: '));
grade2 = int(input('Please enter a grade: '));
grade3 = int(input('Please enter a grade: '));
grade4 = int(input('Please enter a grade: '));
grade5 = int(input('Please enter a grade: '));

total = float(grade1 + grade2 + grade3 + grade4 + grade5)

def calcAverage(total):
   average = float(total/5)
   print('The average grade is ' + average)
   return average

def determineGrade(average):
    if average >= 90:
        print('That is an A.')
    elif average >= 80:
        print('That is a B.')
    elif average >= 70:
        print('That is a C.')
    elif average >= 60:
        print('That is a D.')
    else:
        print('That is a F.')

我并不确切地知道我做错了什么,但是当我尝试更改它时,会出现一个错误,即未定义变量并删除它。我试图调用其他函数并返回变量没有区别。 我使用的是python 3语言。

1 个答案:

答案 0 :(得分:1)

调用您的函数时遇到的唯一错误是average函数中包含print('The average grade is ' + average)的行中的calcAverage需要转换为str。请尝试以下方法:

grade1 = int(input('Please enter a grade: '));
grade2 = int(input('Please enter a grade: '));
grade3 = int(input('Please enter a grade: '));
grade4 = int(input('Please enter a grade: '));
grade5 = int(input('Please enter a grade: '));

total = float(grade1 + grade2 + grade3 + grade4 + grade5)

def calcAverage(total):
   average = float(total/5)
   print('The average grade is ' + str(average))
   return average

def determineGrade(average):
    if average >= 90:
        print('That is an A.')
    elif average >= 80:
        print('That is a B.')
    elif average >= 70:
        print('That is a C.')
    elif average >= 60:
        print('That is a D.')
    else:
        print('That is a F.')

average = calcAverage(total)
determineGrade(average)

样本运行的输出:

Please enter a grade: 89
Please enter a grade: 90
Please enter a grade: 44
Please enter a grade: 56
Please enter a grade: 88
The average grade is 73.4
That is a C.