0

I'm trying to create a mini mad-libs game but came across a Type Error: not enough arguments for format string.

Codecademy apparently teaches an older version of Python so I'm wondering how I can get this to work on Python 3. All the inputs work up until printing the actual story. Here is my code.

STORY = "I was %s at the %s when a %s came along and %s me."

print("Mad Libs has started")

verbing = input("Enter a verb ending in ing: ")

place = input("Enter a place: ")

noun = input("Enter a noun: ")

verb = input("Enter a verb: ")

print (STORY % verbing, place, noun, verb)
awesoon
  • 26,766
  • 9
  • 62
  • 86
Chop
  • 1
  • Possible duplicate of [Python TypeError: not enough arguments for format string](https://stackoverflow.com/questions/11146190/python-typeerror-not-enough-arguments-for-format-string) – awesoon Aug 06 '18 at 04:13
  • Try placing parentheses around `(verbing, place, noun, verb)`. – Frank Aug 06 '18 at 04:13
  • You need to wrap the tuple in parens, or else they are being interpreted as arguments to print – Andrew Li Aug 06 '18 at 04:14

1 Answers1

0

You need to pass a tuple when using % formatting:

print (STORY % (verbing, place, noun, verb))

Note that Python also has an updated syntax for formatting:

STORY = "I was {} at the {} when a {} came along and {} me."
print("Mad Libs has started")
verbing = input("Enter a verb ending in ing: ")
place = input("Enter a place: ")
noun = input("Enter a noun: ")
verb = input("Enter a verb: ")
print (STORY.format(verbing, place, noun, verb))

And Python 3.6+ has "f-strings" where you can input variables directly into the format string, which is much more readable:

print("Mad Libs has started")
verbing = input("Enter a verb ending in ing: ")
place = input("Enter a place: ")
noun = input("Enter a noun: ")
verb = input("Enter a verb: ")
print(f"I was {verbing} at the {place} when a {noun} came along and {verb} me.")
Mark Tolonen
  • 132,868
  • 21
  • 152
  • 208