调试?计算成绩并得到非类型?

时间:2013-03-01 17:11:36

标签: python string debugging

继续收到错误报告

文件“/home/hilld5/DenicaHillPP4.py”,第72行,主要     gradeReport = gradeReport +“\ n”+ studentName +“:\ t%6.1f”%studentScore +“\ t”+ studentGrade TypeError:无法连接'str'和'NoneType'对象

但似乎无法弄清楚为什么学生成绩没有返回

studentName = ""
studentScore = ""

def getExamPoints (total): #calculates Exam grade
total = 0.0
for i in range (4):
    examPoints = input ("Enter exam " + str(i+1) + " score for " + studentName + ": ")
total += int(examPoints)
total = total/.50       
total = total * 100
return total

def getHomeworkPoints (total):#calculates homework grade
total = 0.0
for i in range (5):
    hwPoints = input ("Enter homework " + str(i+1) + " score for " + studentName + ": ")
total += int(hwPoints)
total = total/.10       
total = total * 100
return total

 def getProjectPoints (total): #calculates project grade
total = 0.0
for i in range (3):
    projectPoints = input ("Enter project " + str(i+1) + " score for " + studentName + ": ")
total += int(projectPoints)
total = total/.40       
total = total * 100
return total

def computeGrade (total): #calculates grade
if studentScore>=90:
     grade='A'
elif studentScore>=80 and studentScore<=89:
    grade='B'
    elif studentScore>=70 and studentScore<=79:
        grade='C'
elif studentScore>=60 and studentScore<=69:
        grade='D'
else:
    grade='F'

def main ( ):

classAverage = 0.0      # initialize averages
classAvgGrade = "C"

studentScore = 0.0
classTotal = 0.0
studentCount = 0
gradeReport = "\n\nStudent\tScore\tGrade\n============================\n"

studentName = raw_input ("Enter the next student's name, 'quit' when done: ")

while studentName != "quit":

    studentCount = studentCount + 1

    examPoints = getExamPoints (studentName)
    hwPoints = getHomeworkPoints (studentName)
    projectPoints = getProjectPoints  (studentName)

    studentScore = examPoints + hwPoints + projectPoints

    studentGrade = computeGrade (studentScore)

    gradeReport = gradeReport + "\n" + studentName + ":\t%6.1f"%studentScore+"\t"+studentGrade 

    classTotal = classTotal + studentScore

    studentName = raw_input ("Enter the next student's name, 'quit' when done: ")

print gradeReport

classAverage = int ( round (classTotal/studentCount) )

# call the grade computation function to determine average class grade

classAvgGrade = computeGrade ( classAverage )

print  "Average class score: " , classAverage, "\nAverage class grade: ", classAvgGrade 

main ( ) # Launch

1 个答案:

答案 0 :(得分:1)

computeGrade函数中有两个错误:

  1. 您不会在函数的任何位置引用函数的单个参数(total)。
  2. 该函数没有return语句。
  3. 更正后的版本可能如下所示:

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