TypeError: not all arguments converted during string formatting


#1

Hey! I'm having trouble running this script...it prompts for input properly but after all the prompts I get a TypeError instead of my printed story. Any advice?

The error is "TypeError: not all arguments converted during string formatting" and the code is below. Thanks!

"""
This Python script is designed to build a Madlibs stories based on user input.

Enjoy responsibly.

"""

print ("Hello friend, let\'s start Madlibs!")

name = raw_input("What is your name?: ")
print ("Hello " + name)

Prompt for adjectives below

f_adjective = raw_input("Enter an adjective: ")
s_adjective = raw_input("Enter another adjective: ")
t_adjective = raw_input("Enter one more adjective: ")

Prompt for verbs here

f_verb = raw_input("Time for some action! Enter a verb: ")
s_verb = raw_input("Enter another verb: ")
t_verb = raw_input("Enter one last verb: ")

Prompt the user for nouns below

first_noun = raw_input("Please enter a noun: ")
second_noun = raw_input("Enter a second noun here: ")
third_noun = raw_input("Enter another noun: ")
fourth_noun = raw_input("Enter one more noun: ")

Let's get silly

animal = raw_input("What is your favorite animal: ")
food = raw_input("What is your favorite food: ")
fruit = raw_input("Name a fruit you can eat without peeling: ")
number = raw_input("What is your favorite number: ")
superhero = raw_input("If you could be one superhero, who would it be: ")
country = raw_input("Name a country you would love to travel to: ")
dessert = raw_input("What is your favorite dessert: ")
year = raw_input("What year were you born: ")

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 rythym 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 %s ruled the world."

print STORY % (f_adjective, name, f_verb, s_adjective, first_noun, second_noun, animal, food, s_verb, third_noun, fruit, t_adjective, name, t_verb, number, name, superhero, superhero, name, country, name, dessert, name, name, year, fourth_noun)

Replace this line with your code.


#2

Ah! Got it!

I had to double check and count the number of %s ...originally the number of %s in my story did not match the number of arguments. Corrected code below:

"""

This Python script is designed to build a Madlibs stories based on user input.

Enjoy responsibly.

Thanks :slight_smile: Andrew P.

"""

print ("Hello friend, let\'s start Madlibs!")

name = raw_input("What is your name?: ")
print ("Hello " + name)

Prompt for adjectives below

f_adjective = raw_input("Enter an adjective: ")
s_adjective = raw_input("Enter another adjective: ")
t_adjective = raw_input("Enter one more adjective: ")

Prompt for verbs here

f_verb = raw_input("Time for some action! Enter a verb: ")
s_verb = raw_input("Enter another verb: ")
t_verb = raw_input("Enter one last verb: ")

Prompt the user for nouns below

first_noun = raw_input("Please enter a noun: ")
second_noun = raw_input("Enter a second noun here: ")
third_noun = raw_input("Enter another noun: ")
fourth_noun = raw_input("Enter one more noun: ")

Let's get silly

animal = raw_input("What is your favorite animal: ")
food = raw_input("What is your favorite food: ")
fruit = raw_input("Name a fruit you can eat without peeling: ")
number = raw_input("What is your favorite number: ")
superhero = raw_input("If you could be one superhero, who would it be: ")
country = raw_input("Name a country you would love to travel to: ")
dessert = raw_input("What is your favorite dessert: ")
year = raw_input("What year were you born: ")

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 %ss were many %ss protesting to keep %s in stores. The crowd began to %s to the rythym 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 % (f_adjective, name, f_verb, s_adjective, first_noun, second_noun, animal, food, s_verb, third_noun, fruit, t_adjective, name, t_verb, number, name, superhero, superhero, country, name, dessert, fruit, name, year, fourth_noun)


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.