首页 > 编程语言 > 详细

Python-TypeError: not all arguments converted during string formatting

时间:2018-05-19 23:48:13      阅读:444      评论:0      收藏:0      [点我收藏+]

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=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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!