什么是非类型?我真的不明白

2024-10-17 06:20:25 发布

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

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

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()

它一直在说:

File "/home/hilld5/DenicaHillPP4.py", line 65, in main studentScore = examPoints + hwPoints + projectPoints

TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

我从来没有听说过非类型错误,甚至在google上也没有真正理解。任何认为自己了解发生了什么/知道什么是非类型的人?你知道吗


Tags: heremaindeftotalgradeelifcalculatesstudentscore
2条回答

这只是Python说值是NoneNoneType是“值的类型None”)。你知道吗

它们之所以None是因为函数实际上没有return一个值,所以分配调用函数的结果只是分配None。你知道吗

例如:

>>> def foo():
...   x = 1
...
>>> print foo()
None
>>> def bar():
...   x = 1
...   return x
...
>>> print bar()
1

NoneTypeNone的类型。就这么简单。意思是你在做这样的事情:

a = b = None
c = a + b

相关问题 更多 >