13

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?

5
  • 2
    You have two lines where a format string is used, and the error coude be in either one (although they seem right at first glance). Which one of your lines does the traceback show? Commented May 4, 2010 at 10:40
  • I don't know the line cause this is in server-class error. There is self loggin system. Commented May 4, 2010 at 10:43
  • 1
    no repro. w/o traceback it isn't even possible to see whether it is this particular function causing the error Commented May 4, 2010 at 10:45
  • Please, can you comment one of the lines and check if the error stays? Commented May 4, 2010 at 10:53
  • I know I have similar issues when the format argument is a list, but it usually happens when there are multiple %s. Commented May 4, 2010 at 11:01

2 Answers 2

18

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

2 Comments

it doesn't have to be a tuple in case of a single argument.
@SilentGhost: Right, but if self.turnnow is itself a tuple, there will be a problem. To think, though, it will be more likely "extra arguments".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.