如何调试学生评分脚本中的错误“不支持的操作数类型”?

2024-09-26 22:08:52 发布

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

在这个代码中我做错了什么?我一直在为一个学校作业做这个,每次我认为我已经解决了它,它就会出现这个错误:

 line 63, in averageClaculation
    totalClassScore = totalClassScore + classlist[i][0]
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

这是我的密码:

ACHIEVED = 50
MERIT = 70
EXCELLENCE = 90
testName = ""
classNameLimit = ""
classLimit = ""
classlist = []
highClasslist = []
lowClasslist = []


def grading(studentScore):
if studentScore < ACHIEVED: #If the mark is between 0 and 50 a Not Achived is given
    studentGrade = "Not Achieved"
elif studentScore >= ACHIEVED and studentScore < MERIT:  #If the mark is between 50 and 70 an Acheived is given
    studentGrade = "Achieved"
elif studentScore >= MERIT and studentScore < EXCELLENCE:  #If the mark is between 70 and 90 a merit is given
    studentGrade = "Merit"
elif studentScore >= EXCELLENCE and studentScore <=100:  #If the mark is between 90 and 100 an Excellence is given
    studentGrade = "Excellence"
return studentGrade

def enteringData():
testName = input("What is the name of the test? ")
name = input("What is your name? ").title()
teacher = input("Who is the teacher that conducted the test? ").title
className = input("What is the class name?: ")
classSize = int(input("How many students are in the class? "))
print()
for i in range(0, classSize):
    newStudent = ()
    studentsName = input("Enter student name {0}: ".format(i+1)).title()
    enterScore = True
    while enterScore == True:
        studentScore = int(input("    Enter the score {0} got for the test: ".format(studentsName)))
        if studentScore < 0 or studentScore > 100:
            print("Enter a number between 0 and 100\n")
            studentScore = int(input("    Enter the score {0} got for the test: ".format(studentsName)))
        else:
            break
    studentGrade = grading(studentScore)
    print("      The grade is " +str(studentGrade))
    classlist.append([newStudent])
    print()

def highestCalculation(classlist):
classlist.sort(reverse = True)
topThree = [classlist[0], classlist[1], classlist[2]]
return topThree

def averageClaculation(classlist):
totalClassScore = 0
for i in range(0, len(classlist)):
    totalClassScore = totalClassScore + classlist[i][0]
average = totalClassScore/len(classlist)

def main():
enteringData()
highestCalculation(classlist)
averageClaculation(classlist)
anotherClass = input("Would you like to enter results for another class? 'y' or 'n' ").upper()
if anotherClass == "Y":
    classList = []
    highClasslist = []
    lowClasslist = []

#Main Routine
main()

Tags: andthenameinforinputifis
1条回答
网友
1楼 · 发布于 2024-09-26 22:08:52

异常消息告诉您classlist[i][0]是一个元组,而不是代码部分所期望的整数。要弄清楚为什么会得到错误的类型,需要弄清楚classlist变量是在哪里定义的,以及它的值是如何更新的。你知道吗

发生在enteringData中,您将newstudent定义为空元组,然后稍后执行classlist.append([newStudent])。所以classlist是一个空元组的单元素列表。这几乎肯定不是你想做的。确切地说,您应该保存哪些数据还不清楚,但是您可能希望执行类似于classlist.append([student_score])的操作(在得分之后,列表中可能包含其他值)。您也可以将额外的值放在核心之前,但是您需要更新其他函数来检查适当的索引。您还可以完全摆脱内部列表,只需将classlist作为整数列表(然后使用classlist.append(student_score),并在平均计算中去掉[0]索引)。你知道吗

相关问题 更多 >

    热门问题