zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python学习笔记:TypeError: not all arguments converted during string formatting

Python笔记学习 string not all during arguments
2023-06-13 09:12:45 时间

大家好,又见面了,我是你们的朋友全栈君。

前言

学习python中难免犯下一些幼稚的错误,为了方便后来人的学习与自己的进步,整理了在学习过程中犯下的错误,写下此篇文档。

目录

文章目录

问题

TypeError: not all arguments converted during string formatting

举例

例如:

 strs=(1,2,3,4)  #创建一个集合
 strs
 (1, 2, 3,4)
 >>> print 'strs= %s ' % strs
 Traceback (most recent call last):
   File "<pyshell#43>", line 1, in <module>
     print 'strs= %s ' % str
 TypeError: not all arguments converted during string formatting

原因:1 % 操作符只能直接用于字符串(‘123’),列表([1,2,3])、元组,因此需要一一匹配操作符。

解决方法

 print 'strs= %s' % (strs,)
strs= (1, 2, 3,4)
也可以用:
print 'strs= %s,%s,%s,%s' % sstr
strs= 1,2,3,4 

#简单解释 说明前后%和后面的参数数量不对应,比如

File "<pyshell#37>", line 1, in <module>
print '%f meters is the same as &f km' % (meters, kilometers)
TypeError: not all arguments converted during string formatting

后面有miles和kilometer两个参数,前面只有一个%f,还有一个打印错的&, 前后不一致; 如果改成

print '%f miles is the same as %f km' % (miles, kilometers)

就可以了

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159968.html原文链接:https://javaforall.cn