格式字符串没有足够的参数 [英] Not enough arguments for format string

查看:155
本文介绍了格式字符串没有足够的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有这样的代码:

I have such code in Python:

def send_start(self, player):
    for p in self.players:
        player["socket"].send_cmd('<player id="%s" name="%s" you="%s" avatar="*.png" bank="%s" />'%(self.players.index(p)+1, p['name'], int(player["pid"]==p["pid"]), 0))
    player["socket"].send_cmd('<game playerid="%s" />'%(self.turnnow))
    player["socket"].send_cmd("<start />")

错误在这篇文章的标题中。什么是错误的?

And the error is in the title of this post. What's wrong?

推荐答案

如果 self.turnnow 是一个空元组:

Your code would fail if self.turnnow is an empty tuple:

>>> var = ()
>>> print "%s" % (var)
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: not enough arguments for format string
>>> print "%s" % (var,)
()

如果元组只有一个元素,那么Python中的表达式不会自动成为一个元组。 (expr)相当于 expr (expr,)相当于一个元素元组,它持有 expr 作为第一个元素。因此,请尝试在第二个 print 语句中在 self.turnnow 后添加逗号。

This is because a parenthesized expression in Python does not automatically become a tuple if the tuple would have only one element. (expr) is equivalent to expr. (expr, ) is equivalent to a one-element tuple holding expr as the first element. So, try adding a comma after self.turnnow in the second print statement.

这篇关于格式字符串没有足够的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆