所以我对编程非常陌生,我从 Python 3 开始.我开始阅读Learn Python the Hard Way".现在,我得到了这段代码:
So I am VERY new to programming and I started with Python 3. I started reading "Learn Python the Hard Way". 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我不太清楚%r、%s 和%d 之间的区别.我得到的错误是 TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' 不知道该怎么做以及如何修复它.请解释我如何才能真正使它工作以及为什么它不起作用.另外,%r,d 和 s 有什么区别?任何有用的链接?提前致谢.
I do not really know the difference between %r, %s and %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. 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? Any useful links? Thank you in advance.
推荐答案您想将 % 应用于 string 代替:
You want to apply % to the string instead:
print("I said: %r" % x)您的代码将其应用于 print() 调用的返回值,该调用返回 None.
Your code is applying it to the return value of the print() call, which returns None.
或者,您可以切换到使用 str.format():
Alternatively, you can switch to using str.format():
print("I said: {!r}".format(x))更多推荐
类型错误:% 不支持的操作数类型:'NoneType' 和 'str'
发布评论