在进行数学运算时,<type'nonetype'=“”>问题

时间:2016-02-10 00:45:04

标签: python python-2.7

我有以下代码,但是当我运行它时,我得到'NoneType'类型。我指出我的“求和”变量是具有Nonetype的变量,为什么呢?我怎么能改变它?我尝试制作一个float()但它对我不起作用。任何见解都会受到欢迎。

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
    total = 0
    for item in scores:
        total = item + total
    print total

grades_sum(grades)


def grade_average(grades):
    summation = grades_sum(grades)
    average = summation / float(len(grades))
    return average

print grade_average(grades)

3 个答案:

答案 0 :(得分:4)

将打印更改为返回。

def grades_sum(scores):
    total = 0
    for item in scores:
        total = item + total
    return total

答案 1 :(得分:3)

summation只调用函数,没有别的,所以它等于NoneType或基本上没有。将print total更改为return total,以使summation等于total所有。

def grades_sum(scores):
    total = 0
    for item in scores:
        total = item + total
    return total

答案 2 :(得分:1)

定义def grades_sum(scores): total = 0 for item in scores: total = item + total print total return total 时,不返回任何内容,只需打印即可。将其更改为:

.cpp