0

Note: I searched for an answer on this site, but none of them helped me.

**TypeError: not enough arguments for format string**

So hi, and this is my first hour learning python. I decided to try out what I know already and wrote a code, but an error occurs with this line:

print("'%s' hired '%s' as troop in his first slot." % name, slot1)
Ryan Haining
  • 30,835
  • 10
  • 95
  • 145
Actually Unnamed
  • 123
  • 1
  • 2
  • 6
  • 3
    I don't think you were searching in the right way. Simply putting the error message into [google](https://www.google.ca/#q=typeerror+not+enough+arguments+for+format+string) gives many SO answers, and each of the first three explains the problem. – DSM Feb 14 '15 at 17:48
  • keep your question shorter. It was better before you dumped all of the code. – Ryan Haining Feb 14 '15 at 17:49

2 Answers2

3

You have to provide a tuple argument to the string, otherwise it's only going to take the first thing. when you have:

'%s %s' % a, b

it's parsed similarly (but not exaclty) like

('%s %s' % a), b

meaning the b isn't part of the argument to the % operator. To fix this, parenthesize a and b to give a single tuple argument instead

'%s %s' % (a, b)

In your specific case

"'%s' hired '%s' as troop in his first slot." % (name, slot1)

Though you may want to consider the newer python format syntax

"'{}' hired '{}' as troop in his first slot.".format(name, slot1)
Ryan Haining
  • 30,835
  • 10
  • 95
  • 145
1

The print statement must be

print("'%s' hired '%s' as troop in his first slot." % (name, slot1))
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
  • I'm a bit confused, so on that line I did need to use like this: print("'%s' hired '%s' as troop in his first slot." % (name, slot1)) But on other lines I used the code like this, and no error came: print("Slots left: %s" % slotamount) What is the difference, when to use which? – Actually Unnamed Feb 14 '15 at 17:49
  • @LeDirt When you have a single variable, you don't need to use tuple, whenever there are multiple, you need to put them in a tuple. – Bhargav Rao Feb 14 '15 at 17:53