简体   繁体   English

TypeError:%支持的操作数类型:'NoneType'和'str'

[英]TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

So I am VERY new to programming and I started with Python 3. I started reading "Learn Python the Hard Way". 所以我对编程非常陌生,我从Python 3开始。我开始阅读“以艰难的方式学习Python”。 Now, I got to a point where I had this code: 现在,我得到了一个我有这个代码的地方:

x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s" % (binary, do_not)

print(x)
print(y)
print("I said: %r") % x

I do not really know the difference between %r , %s and %d . 我真的不知道%r%s%d之间的区别。 The error I get is TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' No idea what to do and how to fix it. 我得到的错误是TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'不知道该怎么做以及如何解决它。 Please explain how I can actually make it work and why it won't work. 请解释我如何才能真正使它工作以及为什么它不起作用。 Also, what is the difference between %r,d and s? 另外,%r,d和s之间有什么区别? Any useful links? 任何有用的链接? Thank you in advance. 先感谢您。

You want to apply % to the string instead: 您想要将%应用于字符串

print("I said: %r" % x)

Your code is applying it to the return value of the print() call, which returns None . 您的代码将其应用于print()调用的返回值,该调用返回None

Alternatively, you can switch to using str.format() : 或者,您可以切换到使用str.format()

print("I said: {!r}".format(x))

You are calling the % outside of the print() function. 您正在调用print()函数之外的% This tries to see if the actual function print can be printed as %r , and because print doesn't return anything, it tries to get %r for the value None (hence the NoneType error). 这会尝试查看实际的函数print可以打印为%r ,并且因为print没有返回任何内容,它会尝试获取值%r None %r (因此NoneType错误)。 Change it to: 将其更改为:

print("I said: %r" %(x))

The following code: 以下代码:

#!/usr/local/bin/python3
x = "Hello"
print ("Hello World! %s") %(x)

Raises the following error: 引发以下错误:

Hello World! %s
Traceback (most recent call last):
  File "main.py", line 3, in 
    print ("Hello World! %s") %(x)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

Changing the code to the following works: 将代码更改为以下作品:

#!/usr/local/bin/python3
x = "Hello"
print ("Hello World! %s" %(x))

Output: 输出:

Hello World! Hello

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:+ 不支持的操作数类型:“NoneType”和“str” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' 类型错误:+= 不支持的操作数类型:'NoneType' 和 'str' - TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str' TypeError:+不支持的操作数类型:“ NoneType”和“ str” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' TypeError:&:不支持的操作数类型:“ NoneType”和“ str” - TypeError: unsupported operand type(s) for &: 'NoneType' and 'str' TypeError:+不支持的操作数类型:'NoneType'和'str'+ HOST - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' +HOST TypeError:+的不支持的操作数类型:在执行python脚本时为“ NoneType”和“ str” - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' in executing python script 我有一个 TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' - I am having a TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' Python错误-TypeError:+:'NoneType'和'str'不支持的操作数类型 - Python error - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' CSV阅读器:TypeError:+不支持的操作数类型:“ NoneType”和“ str” - CSV Reader: TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' TypeError:+:使用GITPYTHON的'NoneType'和'str'不受支持的操作数类型 - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str' using GITPYTHON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM