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

路易斯·麦康奈尔

我不了解此错误或其含义。我将代码粘贴在下面,但是我认为它并不重要。我只想了解这个错误。

将所有数字1到1000(包括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)
大卫·赫弗南(David Heffernan)

当所有if测试都不number_translator()为true时,该函数返回None该错误消息是其结果。

每当看到包含以下内容的错误时,'NoneType'这意味着您有一个操作数或一个对象,而该对象正是None您期望其他事物的时候。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Keras报告TypeError:+不支持的操作数类型:'NoneType'和'int'

TypeError:+时不支持的操作数类型:求和列表时为“ int”和“ NoneType”

*:Python 3中不支持*:'NoneType'和'int'的操作数类型

TypeError:打印时%不支持的操作数类型:'NoneType'和'int'

训练GRU模型TypeError时出现此错误:*不支持的操作数类型:'int'和'NoneType'

Python:TypeError:+不支持的操作数类型:“ NoneType”和“ int”

TensorFlow 1.x:TypeError:/:不支持的操作数类型:'NoneType'和'int'

TypeError:%不支持的操作数类型:“ NoneType”和“ int”

+不支持的操作数类型:'NoneType'和'str'和'int'

TypeError:+不支持的操作数类型:“ NoneType”和“ int”

trip_cost('Pittsburgh',5)引发了一个错误:+不支持的操作数类型:'int'和'NoneType'

TypeError:+ =:Python 3中不支持的操作数类型为“ float”和“ NoneType”

TypeError: 不支持的操作数类型 +: 'NoneType' 和 'int' 在 while 循环中

类型错误:* 不支持的操作数类型:'int' 和 'NoneType'

类型错误:不支持 / 的操作数类型:'NoneType' 和 'int',对于extract_image_patches

类型错误:不支持 / 的操作数类型:'NoneType' 和 'float'

类型错误:不支持 + 的操作数类型:'NoneType' 和 'str'/基本转换

不支持的操作数类型 (int)+(var) 错误

+ 不支持的操作数类型:'NoneType' 和 'complex' 错误,而两者都是复杂的

如何修复,TypeError:% 不支持的操作数类型:'NoneType' 和 'int'

递归阶乘报告错误`TypeError:*不支持的操作数类型:'int'和'NoneType'`

python函数处理给出错误TypeError:不支持的操作数类型-:'NoneType'和'NoneType'

出现错误“TypeError:% 不支持的操作数类型:‘NoneType’和‘str’”

类型错误:不支持 + 的操作数类型:最后一行中的“int”和“NoneType”

loguru 中的文字和列表的 Python 错误:+ 不支持的操作数类型:'NoneType' 和 'list'

Python 错误类型错误:+= 不支持的操作数类型:'type' 和 'int'

试图解决“TypeError:+ 不支持的操作数类型:“NoneType”和“int”

* 不支持的操作数类型:“NoneType”和“NoneType”

蟒蛇 | TypeError: 不支持的操作数类型 /: 'NoneType' 和 'int' |使用变量来划分