Python验证函数返回非类型

时间:2017-10-30 19:34:19

标签: python

print ("\n" * 100)
def weight_rep():
    rep = 0
    try:
        rep = int(input("How many times would you like to enter a weight?\n"))
        return rep
    except ValueError:
        print ("\n" * 100)
        print("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
        print("You must enter a valid number that is:\n1) Whole\n2) Positive")
        print("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
        weight_rep()
        return rep

rep = weight_rep()

weight = []

for i in range (0, (rep)):
    weightn = int(input("Please input weight " + str(i+1) +":\n"))
    weight.append(weightn)
    print (weight[i])
mean = sum(weight)/int(len(weight))
print("The average weight of these fruits is {0:.2f}g".format(mean))

如果我在第一阶段输入正确的数字,这个简单的代码会按预期进行,但是,当我输入重复weight_rep()函数然后输入内容的东西时,该函数返回nonetype

1 个答案:

答案 0 :(得分:1)

这些行:

weight_rep()
return rep

应该成为:

return weight_rep()

虽然我不建议你做过递归调用,但上面的内容将解决你当前的问题。