1

为帖子简化了它(大多数“”是我程序中功能齐全的实际代码):

studentName = ""

def getExamPoints (total):

"calculates examPoints here"

def getHomeworkPoints (total):
"calculates hwPoints here"

def getProjectPoints (total):
"calculates projectPoints here"

def computeGrade ():
if studentScore>=90:
     grade='A'
elif studentScore>=80:
        grade='B'
elif studentScore>=70:
        grade='C'
elif studentScore>=60:
        grade='D'
else:
    grade='F'


def main():

classAverage = 0.0      # All below is pre-given/ required code
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 #(<---- heres where my problem is!)

    studentGrade = computeGrade (studentScore)


main()

它一直在说:

文件“/home/hilld5/DenicaHillPP4.py”,第 65 行,在 main studentScore =examPoints + hwPoints + projectPoints

类型错误:+ 不支持的操作数类型:“NoneType”和“NoneType”

我从未了解或听说过非类型错误,即使在谷歌搜索时也没有真正理解。任何认为他们了解正在发生的事情/知道什么是非类型的人?

4

2 回答 2

4

这只是 Python 的说法,即值是NoneNoneType是“值的类型None”)。

它们的原因None是因为您的函数实际上并不是return一个值,因此分配调用函数的结果只是 assigns None

举个例子:

>>> def foo():
...   x = 1
...
>>> print foo()
None
>>> def bar():
...   x = 1
...   return x
...
>>> print bar()
1
于 2013-03-01T07:03:27.023 回答
1

NoneType是 的类型None。就那么简单。这意味着你正在做这样的事情:

a = b = None
c = a + b
于 2013-03-01T07:04:17.910 回答