python3.x元组打印错误 TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
原创by南山南北秋悲
欢迎引用!请注明原地址:http://www.cnblogs.com/hwd9654/p/5676746.html 谢谢!
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
打印变量时遇到的问题,
想着c语言中printf("%s", 变量名)
python中 应该也是类似的语句
果断百度,发现是通过语句后附加一个%(变量名)来解决的
详细如下:
print语句可以使用跟着%符号的项目元组的字符串。这些字符串具备定制的功能。定制让输出满足某种特定的格式。定制可以是%s表示字符串或%d表示整数。元组必须按照相同的顺序来对应这些定制。
观察我们使用的第一个元组,我们首先使用%s,这对应变量name,它是元组中的第一个项目。而第二个定制是%d,它对应元组的第二个项目age。
Python在这里所做的是把元组中的每个项目转换成字符串并且用字符串的值替换定制的位置。因此%s被替换为变量name的值,依此类推。
print的这个用法使得编写输出变得极其简单,它避免了许多字符串操作。它也避免了我们一直以来使用的逗号。
在大多数时候,你可以只使用%s定制,而让Python来提你处理剩余的事情。这种方法对数同样奏效。然而,你可能希望使用正确的定制,从而可以避免多一层的检验程序是否正确。
在第二个print语句中,我们使用了一个定制,后面跟着%符号后的单个项目——没有圆括号。这只在字符串中只有一个定制的时候有效。
例子:
age = 22name = 'Swaroop'
print '%s is %d years old' % (name, age)print 'Why is %s playing with that python?' % name
结果:
Swaroop is 22 years old
Why is Swaroop playing with that python?
可是我自己编程的时候却遇到了上述问题:TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
语句如下:
a = 'Peter'
b = 'Linda' print("who is the murder? %s or %s?") % (a, b) 后来才发现,python3.x与python2.x有一点区别,
原来%(变量名,...)应该是加在print括号里的
如:print("who is the murder? %s or %s" % (a, b)) 改完以后输出正常:

python3.x元组打印错误 TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'的更多相关文章
- [bug] TypeError : unsupported operand type(s) for += : 'NoneType' and 'int'
原因 Flask购物网站中,每点击货物一次,数据库中货物的浏览次数+1,默认浏览次数为NULL,故无法完成运算 解决 将数据库中相应字段默认值设为0,注意要先断开数据库连接
- TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal'
TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal' 浮点型和双精度类型 相加报错 from deci ...
- python pip install 报错TypeError: unsupported operand type(s) for -=: 'Retry' and 'int' Command "python setup.py egg_info" failed with error code 1 in
pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl ...
- python TypeError: unsupported operand type(s) for +: 'int' and 'str' [closed]
TypeError: unsupported operand type(s) for +: 'int' and 'str' [closed] sql="insert into auto_tr ...
- python TypeError: unsupported operand type(s) for +: 'geoprocessing value object' and 'str'
TypeError: unsupported operand type(s) for +: 'geoprocessing value object' and 'str' if self.params[ ...
- TypeError: unsupported operand type(s) for |: 'str' and 'str'
问题描述:
- PyCharm启动报错 TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’ 解决
这个提示大概是说:"类型错误:不支持操作类型为字符串和字符串",直接把两个字符串(BASE_DIR = os.path.dirname(os.path.dirname(os.pat ...
- 解决pip安装时出现报错TypeError unsupported operand type(s) for -= 'Retry' and 'int'
1.参考 https://stackoverflow.com/questions/42610545/typeerror-unsupported-operand-types-for-retry-and- ...
- pip 下载时出现问题TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'
我这里解决就是更新下载源,马德,中科的源居然不够快,我就只能换源了,一换就成功了 1.一次性(临时使用): 可以在使用pip的时候加参数-i https://pypi.tuna.tsinghua.ed ...
随机推荐
- ”在活动中穿梭”已经重做为“Intent的使用”
更新地址:http://www.cnblogs.com/tangwanzun/p/5702276.html
- https单向认证和双向认证区别
关于证书 1.每个人都可以使用一些证书生成工具为自己的站点生成证书(比如jdk的keytool),大家称它为“自签名证书”,但是自己生成的证书是不被互联网承认的,所以浏览器会报安全提示,要求你手动安装 ...
- iOS开发之UIView的常见属性
1.所有控件都继承自UIView,UIView的常见属性如下: @property(nonatomic,readonly) UIView *superview;获得自己的父控件对象 @property ...
- CSS.06 -- 尚合网页模拟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ASP.NET Core:使用Dapper和SwaggerUI来丰富你的系统框架
一.概述 1.用VS2017创建如下图的几个.NET Standard类库,默认版本为1.4,你可以通过项目属性进行修改,最高支持到1.6,大概五月份左右会更新至2.0,API会翻倍,很期待! 排名分 ...
- 计算两个date类型的时间差
//两个时间之间分钟差 public static int compareDate(Date d1, Date d2) { // TODO Auto-generated method s ...
- freemarker导出word文档——WordXML格式解析
前不久,公司一个项目需要实现导出文档的功能,之前是一个同事在做,做了3个星期,终于完成了,但是在项目上线之后却发现导出的文档有问题,此时,这个同事已经离职,我自然成为接班者,要把导出功能实现,但是我看 ...
- OpenCV局部变形算法探究
OpenCV是跨平台的强大的计算机视觉识别和图像处理的开源库,可以利用他来实现:模式识别.构建神经网络.深度学习,总之用途多多,入门级就先做一下图像处理吧! 基本的图像处理算法(图像灰阶化.二值化.仿 ...
- 老李分享:android app自动化测试工具合集
老李分享:android app自动化测试工具合集 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨 ...
- 一个GOOD的Idea需要伯乐发觉-致敬错过的IDEA
类似一个微信电话本