调试?计算等级并得到类型?

2024-06-16 14:11:40 发布

您现在位置:Python中文网/ 问答频道 /正文

不断获取错误报告

文件“/home/hilld5/DenicaHillPP4.py”,第72行,在main中 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

Tags: forinputdefinttotalgradescoreenter
1条回答
网友
1楼 · 发布于 2024-06-16 14:11:40

computeGrade函数中有两个错误:

  1. 不能在函数中的任何地方引用函数的单个参数(total)。你知道吗
  2. 函数没有return语句。你知道吗

更正的版本可能如下所示:

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

相关问题 更多 >