Stuck on Python Mad Libs


#1

Hello,

im stuck on the Mad libs activity when I try to run the activity I keep getting an error 'not enough arguments for format string" Ive given my answers too many times & am turning to the forum for help. I cannot find the error in my code:

“”"This program does
the following…

Author: Laura"""
print "Mad Libs is starting!"
name = raw_input("Enter a name: ")
adj_1 = raw_input("Enter an adjective: ")
adj_2 = raw_input("Enter a second adjective: ")
adj_3 = raw_input("Enter a third adjective: ")
verb_1 = raw_input("Enter a verb: ")
verb_2 = raw_input("Enter a second verb: ")
verb_3 = raw_input("Enter a third verb: ")
noun_1 = raw_input("Enter a noun: ")
noun_2 = raw_input("Enter another noun: ")
noun_3 = raw_input("Enter a third noun: ")
noun_4 = raw_input("Enter a fourth 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 name: ")
country = raw_input("Enter a country: ")
dessert = raw_input("Enter a dessert: ")
year = raw_input("Enter a year: ")
number = raw_input("Enter a number: ")

#The template for the story
STORY = “This morning I woke up and felt %s because %s was going to finally %s over the big %s %s. On the other side of the %s were many %s protesting to keep %s in stores. The crowd began to %s to the rhythm of the %s, which made all of the %s very %s. %s tried to %s into the sewers and found %s rats. Needing help, %s quickly called %s. %s appeared and saved %s by flying to %s and dropping %s into a puddle of %s. %s then fell asleep and woke up in the year %s, in a world where %ss ruled the world.”

print STORY “%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s” % (adj_1, name, verb_1, adj_2, noun_1, noun_2, animal, food, verb_2, noun_3, fruit, adj_3, name, verb_3, number, name, superhero, superhero, name, country, name, dessert, name, year, noun_4)

Please leave the following link in the post so we can find the unit module, else replace it with a link to the exact lesson:

Strings and Console Output


#2

Hi @lkfalck,

What is the purpose of this? …

“%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s” 

#3

Here’s what I ended up doing and it worked!

print STORY % (adj_1, name, verb_1, adj_2, noun_1, noun_2, animal, food, verb_2, noun_3, fruit, adj_3, name, verb_3, number, name, superhero, superhero, name, country, name, dessert, name, year, noun_4)


#4

The reason it now works is that the number of %s format specifiers in STORY matches the number of variables in the tuple that follows the % operator. You did not need this string …

“%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s”

EDITED (December 5, 2017) to revise the following explanation:

Within the print statement in your original post, STORY is followed by a space and then the format string cited above. That would raise a SyntaxError. To generate the message …

not enough arguments for format string

… a previous version of your code must have included a format string with more %s format specifiers than you had intended. Perhaps you had concatenated STORY with the preceding format string. In that case, there would not have been a sufficient number of variables to match all of the format specifiers.


#5