格式字符串参数不足
我在 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 />")
错误在这篇文章的标题中。怎么了?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 self.turnnow 是一个空元组,您的代码将会失败:
这是因为如果元组只有一个元素,Python 中带括号的表达式不会自动成为元组。
(expr)
相当于expr
。(expr, )
相当于将expr
作为第一个元素的单元素元组。因此,请尝试在第二个print
语句中的self.turnnow
后面添加一个逗号。Your code would fail if
self.turnnow
is an empty tuple: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 toexpr
.(expr, )
is equivalent to a one-element tuple holdingexpr
as the first element. So, try adding a comma afterself.turnnow
in the secondprint
statement.编辑:忽略这个答案,这不可能是问题。留待评论。
尝试是否
用
help 替换(即添加尾随逗号)。现在的情况不是元组,括号只是装饰性的。情况可能并非如此,因为您没有提供行号 - 必须猜测。
EDIT: Disregard this answer, it cannot be the problem. Keeping for the comments.
Try if replacing
with
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.