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)
2
  • 3
    I don't think you were searching in the right way. Simply putting the error message into google gives many SO answers, and each of the first three explains the problem. Commented Feb 14, 2015 at 17:48
  • keep your question shorter. It was better before you dumped all of the code. Commented Feb 14, 2015 at 17:49

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

The print statement must be

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

2 Comments

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?
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.