类型错误:% 不支持的操作数类型:'NoneType' 和 'str'

编程入门 行业动态 更新时间:2024-10-14 18:20:41
本文介绍了类型错误:% 不支持的操作数类型:'NoneType' 和 'str'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我对编程非常陌生,我从 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'

本文发布于:2023-10-28 04:34:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535535.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   不支持   错误   操作   str

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!