原创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 = 22
name = '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'的更多相关文章

  1. [bug] TypeError : unsupported operand type(s) for += : 'NoneType' and 'int'

    原因 Flask购物网站中,每点击货物一次,数据库中货物的浏览次数+1,默认浏览次数为NULL,故无法完成运算 解决 将数据库中相应字段默认值设为0,注意要先断开数据库连接

  2. TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal'

    TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal' 浮点型和双精度类型 相加报错 from deci ...

  3. 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 ...

  4. 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 ...

  5. 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[ ...

  6. TypeError: unsupported operand type(s) for |: 'str' and 'str'

    问题描述:

  7. PyCharm启动报错 TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’ 解决

    这个提示大概是说:"类型错误:不支持操作类型为字符串和字符串",直接把两个字符串(BASE_DIR = os.path.dirname(os.path.dirname(os.pat ...

  8. 解决pip安装时出现报错TypeError unsupported operand type(s) for -= 'Retry' and 'int'

    1.参考 https://stackoverflow.com/questions/42610545/typeerror-unsupported-operand-types-for-retry-and- ...

  9. pip 下载时出现问题TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

    我这里解决就是更新下载源,马德,中科的源居然不够快,我就只能换源了,一换就成功了 1.一次性(临时使用): 可以在使用pip的时候加参数-i https://pypi.tuna.tsinghua.ed ...

随机推荐

  1. Unity3D Shader Stencil模版测试学习

    官方文档地址: https://docs.unity3d.com/Manual/SL-Stencil.html 参考博客: http://blog.csdn.net/onafioo/article/d ...

  2. Linux环境Perl链接MS Sql Server数据库

    1.下载相关软件 unixODBC.freetds和DBD-ODBC ①.Linux系统的ODBC unixODBC-2.3.4.tar.gz ( http://www.unixodbc.org) ② ...

  3. geoR文档翻译

    说来惭愧,很久没有更新自己的博客了.期间个人生活经历了很多变故,心理上的打击尤甚.加之没有取得好的科研成果,痛定思痛,还是下苦功夫多多学习. 最近对比验证各种方法的插值精度,用到了R语言地统计学包,由 ...

  4. SharePoint 切换用户的小技巧

    前言 从SharePoint 2013开始,SharePoint就已经去掉了”Sign in as Different User”这个功能,也就是无法切换用户登录.当然,后来我们通过修改CONTROL ...

  5. java学习笔记 --- 多态

    一.多态 (1)定义:同一个对象在不同时刻体现出来的不同状态.父类的引用或者接口的引用指向了自己的子类对象.   Dog d = new Dog();//Dog对象的类型是Dog类型.  Animal ...

  6. java与xml之间的转换(jaxb)

    使用java提供的JAXB来实现java到xml之间的转换,先创建两个持久化的类(Student和Classroom): Classroom: package com.model; public cl ...

  7. python大小写转换函数

    1.全部转换成大写:upper() 用法: str = 'marsggbo'     print str.upper() 结果:MARSGGBO 2.全部转换成小写:lower() 用法:str = ...

  8. git clone操作到开发机的错误记录

    在开发机上,执行操作 $ git clone https://github.com/xxx/rank.git 返回错误: error: The requested URL returned error ...

  9. PT20150801隆重开班

    PT20150801隆重开班 伴随着秋天的的脚步,带着对梦想的憧憬,POPTEST1508期学员步入正式学习阶段:POPTEST的课程内容吸收了互联网公司先进技术的特点,同时坚持深入浅出的教育特点,完 ...

  10. backtracking问题

    backtracking最基础的问题是Subsets,即给定一个数组,要求返回其所有子集. Given a set of distinct integers, nums, return all pos ...