我在这段代码中做错了什么?我一直在为这个学校作业工作,每当我想我已经修好它时,就会出现这个错误:
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()
答案 0 :(得分:0)
异常消息告诉您classlist[i][0]
是一个元组,而不是代码部分所需的整数。要弄清楚您输入错误类型的原因,您需要确定classlist
变量的定义位置以及它的值如何更新。
这发生在enteringData
中,您将newstudent
定义为空元组,然后执行classlist.append([newStudent])
。所以classlist
是空元组的单元素列表的列表。这几乎肯定不是你想做的事情。确切地说,您应该保存的数据并不清楚,但可能您想要执行classlist.append([student_score])
之类的操作(在分数之后列表中可能包含其他值)。您还可以将额外的值放在核心之前,但是您需要更新其他函数以检查相应的索引。您也可以完全摆脱内部列表,只需将classlist
作为整数列表(然后使用classlist.append(student_score)
,并删除平均值中的[0]
索引计算)。