Error with Madlibs.py

"""
This program generates passages that are generated in mad-lib format
Author: Katherin 
"""

# The template for the story
STORY = "This morning %s woke up feeling %s. 'It is going to be a %s day!' Outside, a bunch of %s's were protesting to keep %s in stores. They began to %s to the rhythm of the %s, which made all the %s's very %s. Concerned, %s texted %s, who flew %s to %s and dropped %s in a puddle of frozen %s. %s woke up in the year %s, in a world where %s ruled the world."


print "The story will begin now."

name = raw_input("Enter a name: ")
adj1 = raw_input("Enter an adjective: ")
adj2 = raw_input("Enter a second adjective: ")
adj3 = raw_input("Enter one more adjective: ")
verb = raw_input("Enter a Verb: ")
noun1 = raw_input("Enter a noun: ")
noun2 = raw_input("Enter a second noun: ")
animal = raw_input("Enter an animal: ")
food = raw_input("Enter a food: ")
fruit = raw_input("Enter a fruit: ")
superhero = raw_input("Enter a superhero: ")
country = raw_input("Enter a country: ")
dessert = raw_input("Enter a dessert: ")
year = raw_input("Enter a year: ")



print STORY % ('name, adj1, adj2, animal, food, verb, noun1, fruit, adj3, name, superhero, name, country, name, dessert, name, year, noun2')

Running this code returns TypeError: not enough arguments for format string

I’ve looked over the code repeatedly and can’t find a solution. I read it may be because there’s not an equal amount of %s to the variables in the print command, but I believe they both have 18?

1 Like

Do the variables need to be set to strings.
for example if I type 2050 wouldn’t it be read as a number instead of a string.

1 Like

Welcome to the forum!

It’s because in this line:
print STORY % ('name, adj1, adj2, animal, food, verb, noun1, fruit, adj3, name, superhero, name, country, name, dessert, name, year, noun2')

You’ve wrapped the variables which are to replace the placeholders in single quotes. Python is interpreting that as you giving it a single value to replace the first placeholder, and nothing to replace the rest.

Remove the ' ' from inside the brackets and your code works fine.

1 Like

No, because the raw_input() function always returns a string.

If you wanted a number, you’d need to convert it afterwards. :slight_smile:

2 Likes

Thank you sir or ma’am, you just increased my code knowledge, while answering their question
:smile:

1 Like

No, when the user inputs it into the console the raw_input function automatically takes that input as a string.

1 Like

ahhh, thank you! That fixed it.

1 Like

No problem.

I also initially missed it… :sweat_smile:

I did take a slightly excessive means of checking that you had the same number of placeholders and values, though… (It probably wasn’t any quicker than adjusting your code to run in Python 3 and then generating the story, but it was a neat challenge.)

STORY = "This morning %s woke up feeling %s. 'It is going to be a %s day!' Outside, a bunch of %s's were protesting to keep %s in stores. They began to %s to the rhythm of the %s, which made all the %s's very %s. Concerned, %s texted %s, who flew %s to %s and dropped %s in a puddle of frozen %s. %s woke up in the year %s, in a world where %s ruled the world."
pfcount = 0

while True:
    idx = STORY.find("%s")
    if idx == -1:
        break
    else:
        STORY = STORY[idx+2:]
        pfcount += 1

print("There are %d instances of printf placeholders in STORY!" % pfcount)

strvars = "name, adj1, adj2, animal, food, verb, noun1, fruit, adj3, name, superhero, name, country, name, dessert, name, year, noun2"
varlst = [x.strip() for x in strvars.split(",")]

print("There are %d variables in the printf placeholders!" % len(varlst))

if pfcount == len(varlst):
    print("Same number of variables as placeholders!")
else:
    print("Different number of variables as placeholders!")
1 Like