Not enough arguments for format string

独自空忆成欢 提交于 2019-12-12 07:45:34

问题


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?


回答1:


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,)
()

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.




回答2:


EDIT: Disregard this answer, it cannot be the problem. Keeping for the comments.

Try if replacing

(self.turnnow)

with

(self.turnnow,)

helps (i.e. adding a trailing comma). The way it is now that's not a tuple and parens are merely decorative. Might not be the case since you didn't provide line number — have to guess.



来源:https://stackoverflow.com/questions/2764520/not-enough-arguments-for-format-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!