Python-TypeError: not all arguments converted during string formatting

此页面是否是列表页或首页?未找到合适正文内容。

Python-TypeError: not all arguments converted during string formatting

标签:reterrorfamilyelseoat使用for报错pre

Where?

  运行Python程序,报错出现在这一行return \”Unknow Object of %s\” % value

Why?

   %s表示把 value变量装换为字符串,然而value值是Python元组,Python中元组不能直接通过%s和 %对其格式化,则报错

Way?

  使用 format或 format_map代替 %进行格式化字符串

出错代码

def use_type(value):
if type(value) == int:
return \”int\”
elif type(value) == float:
return \”float\”
else:
return \”Unknow Object of %s\” % value

if __name__ == ‘__main__‘:
print(use_type(10))
# 传递了元组参数
print(use_type((1, 3)))

改正代码

def use_type(value):
if type(value) == int:
return \”int\”
elif type(value) == float:
return \”float\”
else:
# format 方式
return \”Unknow Object of {value}\”.format(value=http://www.mamicode.com/value)
# format_map方式
# return\”Unknow Object of {value}\”.format_map({
# \”value\”: value
# })

if __name__ == ‘__main__‘:
print(use_type(10))
# 传递 元组参数
print(use_type((1, 3)))

  

Python-TypeError: not all arguments converted during string formatting

标签:reterrorfamilyelseoat使用for报错pre

原文地址:https://www.cnblogs.com/2bjiujiu/p/9062115.html

作者: 雨林木风

为您推荐

返回顶部