鱼C论坛

 找回密码
 立即注册
查看: 1938|回复: 19

[已解决]Type问题

[复制链接]
发表于 2019-7-28 11:25:40 | 显示全部楼层 |阅读模式
4鱼币
  1. def printAll(card):
  2.         print("name\tage\tqq\tweixin\t\taddr")
  3.         for temp in card:
  4.                 print("%s\t%s\t%s\t%s\t%s\t\t%s"%(temp['name'],temp['age'],temp['qq'],temp['weixin'],temp['addr']))
复制代码


Error:not enough arguments for format string
求助,谢谢
最佳答案
2019-7-28 11:25:41
本帖最后由 XiaoPaiShen 于 2019-8-30 12:07 编辑
  1. def printAll(card):
  2.     print("name\tage\tqq\tweixin\t\taddr")
  3.     for temp in card:
  4.         print("%s\t%s\t%s\t%s\t\t%s"%(temp['name'],temp['age'],temp['qq'],temp['weixin'],temp['addr'])) # 原来在该语句中多写了一个%s

  5. user1 = {'name':'小甲鱼', 'age':22, 'qq':'12345', 'weixin':'sameph', 'addr': '中国北京'}
  6. user2 = {'name':'小螃蟹', 'age':20, 'qq':'12345', 'weixin':'idontk', 'addr': '广东深圳'}

  7. card = [user1, user2]

  8. printAll(card)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-28 11:25:41 | 显示全部楼层    本楼为最佳答案   
本帖最后由 XiaoPaiShen 于 2019-8-30 12:07 编辑
  1. def printAll(card):
  2.     print("name\tage\tqq\tweixin\t\taddr")
  3.     for temp in card:
  4.         print("%s\t%s\t%s\t%s\t\t%s"%(temp['name'],temp['age'],temp['qq'],temp['weixin'],temp['addr'])) # 原来在该语句中多写了一个%s

  5. user1 = {'name':'小甲鱼', 'age':22, 'qq':'12345', 'weixin':'sameph', 'addr': '中国北京'}
  6. user2 = {'name':'小螃蟹', 'age':20, 'qq':'12345', 'weixin':'idontk', 'addr': '广东深圳'}

  7. card = [user1, user2]

  8. printAll(card)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-7-28 12:00:59 | 显示全部楼层

qiu

TypeError: not enough arguments for format string
  1. def printAll(card):
  2.         print("name\tage\tqq\tweixin\t\taddr")
  3.         for temp in card:
  4.                 print("%s\t%s\t%s\t%s\t%s\t\t%s"%(temp['name'],temp['age'],temp['qq'],temp['weixin'],temp['addr']))
复制代码

以上代码有问题,求助,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-28 18:22:04 | 显示全部楼层
完全没搞懂你在干什么
  1. >>> def printAll(**card):
  2.         print("name\tage\tqq\tweixin\t\taddr")
  3.         print("{name}\t{age}\t{qq}\t{weixin}\t\t{addr}".format(**card))

  4.         
  5. >>> printAll(name='XXX', age=0, qq='XXX', weixin='XXX', addr='...')
  6. name        age        qq        weixin                addr
  7. XXX        0        XXX        XXX                ...
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-28 20:06:10 | 显示全部楼层
juhugufudu 发表于 2019-7-28 12:00
TypeError: not enough arguments for format string

以上代码有问题,求助,谢谢
  1. def printAll(card):
  2.         print("name\tage\tqq\tweixin\t\taddr")
  3.         for temp in card:
  4.                 print("%s\t%s\t%s\t%s\t%s\t"%(temp['name'],temp['age'],temp['qq'],temp['weixin'],temp['addr']))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-29 09:07:21 | 显示全部楼层
本帖最后由 zltzlt 于 2019-7-29 09:10 编辑

你也可以这样:

  1. def printAll(card):
  2.     print("name\tage\tqq\tweixin\t\taddr")
  3.     for temp in card:
  4.         print("{}\t{}\t{}\t{}\t{}\t".format(temp['name'], temp['age'], temp['qq'], temp['weixin'], temp['addr']))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-29 09:07:27 | 显示全部楼层
你应该是是想让第一个print的内容对应上for循环后面的内容的吧?可是如果是这样的,你的参数你得设置啊,单单print出来也只是显示出来的对象,而不是实际的对象,我觉得你可以设置下参数嘛,或者用字典来表示,更加清晰明了,个人建议,喜欢就试试,不喜欢只当没看到就行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-29 11:35:37 | 显示全部楼层
print里面6个%s,后面只给了5个参数,提示你给的参数不够
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-8 14:08:55 | 显示全部楼层
def printAll(card):
        print("name\tage\tqq\tweixin\t\taddr")
        for temp in card:
                print("%s\t%s\t%s\t%s\t%s\t"%(temp['name'],temp['age'],temp['qq'],temp['weixin'],temp['addr']))
应是这个
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-24 15:11:38 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-26 08:40:31 | 显示全部楼层

@不二如是 干什么?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-26 08:42:46 | 显示全部楼层
_2_ 发表于 2019-8-26 08:40
@不二如是 干什么?

没干什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-26 08:43:35 | 显示全部楼层

我又@了一遍......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-26 14:24:53 | 显示全部楼层
怎么不设置一个最佳答案呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-27 20:07:08 | 显示全部楼层
错误信息你去翻译一下就知道了啊……
格式字符串的参数不足
——百度翻译
参数没给够,或者格式字符串的格式化符号多了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-28 09:27:12 | 显示全部楼层
flamer 发表于 2019-7-29 11:35
print里面6个%s,后面只给了5个参数,提示你给的参数不够

题主只是想按照格式来循环打印列表中的内容而已,真正报错的原因,才是这个兄弟说的,格式化输出时,变量和参数的个数对应不起来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-29 10:24:54 | 显示全部楼层
  1. def printAll(card):
  2.         print("name\tage\tqq\tweixin\t\taddr")
  3.         for temp in card:
  4.                 print("%s\t%s\t%s\t%s\t"%(temp['name'],temp['age'],temp['qq'],temp['weixin'],temp['addr']))
复制代码

这样应该可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-30 10:36:35 From FishC Mobile | 显示全部楼层
zltzlt 发表于 2019-8-26 14:24
怎么不设置一个最佳答案呢

TA肯定

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-8-30 10:58:51 | 显示全部楼层
本帖最后由 zltzlt 于 2019-8-30 12:13 编辑

当我没 @
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 01:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表