Python 错误:+ 不支持的操作数类型:'int' 和 'NoneType'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24110282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Error: unsupported operand type(s) for +: 'int' and 'NoneType'
提问by louie mcconnell
I don't understand this error OR what it means. I will paste my code underneath, but I don't think it is really relevant; I just want to understand this error.
我不明白这个错误或它意味着什么。我会将我的代码粘贴在下面,但我认为这并不重要;我只是想了解这个错误。
It's just a bit of code to add up the letters in all numbers 1 - 1000 (inclusive)
把所有数字1 - 1000(含)中的字母相加只需一点代码
def number_translator(x):
if x == 1:
return 3
elif x == 2:
return 3
elif x == 3:
return 5
elif x == 4:
return 4
elif x == 5:
return 4
elif x == 6:
return 3
elif x == 7:
return 5
elif x == 8:
return 5
elif x == 9:
return 4
elif x == 10:
return 3
elif x == 11:
return 6
elif x == 12:
return 6
elif x == 14:
return 8
elif x == 15:
return 7
elif x == 16:
return 7
elif x == 17:
return 9
elif x == 18:
return 8
elif x == 19:
return 8
elif x == 20:
return 6
elif x == 30:
return 6
elif x == 40:
return 5
elif x == 50:
return 5
elif x == 60:
return 5
elif x == 70:
return 7
elif x == 80:
return 6
elif x == 90:
return 6
count = 0
for element in range(1, 1001):
if element < 21:
count += number_translator(element) # for numbers 1 - 20
elif 20 < element < 100:
count += number_translator(int(str(element)[0]) * 10) + number_translator(int(str(element)[1])) # for numbers 21 through 100
elif element % 100 == 0 and element != 1000:
count += number_translator(int(str(element)[0])) + 7 # for numbers divisible by 100, but not 1000
elif element == 1000:
count += 11 # just for 1000
elif element % 100 < 20:
count += number_translator(int(str(element)[0])) + 10 + number_translator(int(str(element)[1:3])) # now I add in numbers like 101 - 120, 201 - 220, etc.
else:
count += number_translator(int(str(element)[0])) + 10 + number_translator(int(str(element)[1]) * 10) + number_translator(int(str(element)[2])) # now the rest( 121, 122, 123, 225, 256, 984, etc.)
print(count)
采纳答案by David Heffernan
When none of the if
test in number_translator()
evaluate to true, the function returns None
. The error message is the consequence of that.
当评估中的任何if
测试都不number_translator()
为真时,函数返回None
。错误消息是结果。
Whenever you see an error that include 'NoneType'
that means that you have an operand or an object that is None
when you were expecting something else.
每当您看到包含错误的错误时,'NoneType'
就意味着您有一个操作数或对象,而这正是None
您期待其他内容的时候。
回答by user2357112 supports Monica
In your giant elif
chain, you skipped 13. You might want to throw an error if you hit the end of the chain without returning anything, to catch numbers you missed and incorrect calls of the function:
在你的巨型elif
链中,你跳过了 13。如果你在没有返回任何东西的情况下到达链的末尾,你可能想抛出一个错误,以捕获你错过的数字和函数的错误调用:
...
elif x == 90:
return 6
else:
raise ValueError(x)