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

Excetera

代码:

String1 = " Is right"
g = lambda x: x ** 2 
print(g(8)) + str(String1)

错误:

Traceback (most recent call last):
  File "C:/Users/Computer/Desktop/Testing.py", line 3, in <module>
    print(g(8)) + str(String1)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

代码2:我也尝试添加此代码:

g = lambda x: x ** 2  + " Should be right!"
print(g(8))

错误:

Traceback (most recent call last):
  File "C:/Users/Computer/Desktop/Testing.py", line 2, in <module>
    print(g(8))
  File "C:/Users/Computer/Desktop/Testing.py", line 1, in <lambda>
    g = lambda x: x ** 2  + " Should be right!"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

我用int和str尝试过,但是仍然有问题吗?

另外,修复时,请解释固定代码的工作原理:)我不想只复制固定的行

马丁·彼得斯(Martijn Pieters)

您正在将print()and的结果相加str(),但会print()返回None

您可能想打印出addg(8)的结果str(),但是您也必须将return的值g(8)转换为字符串:

print(str(g(8)) + str(String1))

请注意)print()函数的关闭位置

str()根本不需要第二个调用,因为String1已经是一个字符串:

print(str(g(8)) + String1)

您可以print()通过将值作为单独的参数传递来代替所有这些操作:

String1 = "Is right"
print(g(8), String1)

我删除了开头的空格,String1因为print()默认情况下,将在要打印的参数之间插入空格。

演示:

>>> String1 = "Is right"
>>> g = lambda x: x ** 2 
>>> print(g(8), String1)
64 Is right

您的第二次尝试试图将字符串连接移动到glambda上。在这种情况下,您必须x ** 2先将结果转换为字符串:

g = lambda x: str(x ** 2)  + " Should be right!"

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

+不支持的操作数类型(“ str”和“ int”)

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

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

TypeError:&:不支持的操作数类型:“ NoneType”和“ str”

TypeError: 不支持的操作数类型 +: 'NoneType' 和 'str'"?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

类型错误:不支持 - 的操作数类型:'str' 和 'int' [Python]

类型错误:不支持 / 的操作数类型:'str' 和 'int'

类型错误:不支持 str 和 int 的操作数类型

Tkinter:类型错误:+ 不支持的操作数类型:'int' 和 'str'

类型错误:+ 不支持的操作数类型:'int' 和 'str' - Pandas DataFrame

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

您能否检查“-不支持的操作数类型:'str'和'int'”

Seaborn boxplot:TypeError:/:'str'和'int'不支持的操作数类型