Codeacademy Python 4/11 MadLibs

So I am going through CodeAcademy’ Python Course and am currently on 4/11 — the one where you get to write a Mad Libs story. Tried to run it after completing my code, but kept getting error message. I have attached my stuff here, any help appreciated … thanks alot !

MadLibs.py

This program does the following ... writes a Mad Libs story

Author: Cai Gengyang

print "Mad Libs is starting!"

name = raw_input("Enter a name: ")

Adjective1 = raw_input("Enter an adjective: ")
Adjective2 = raw_input("Enter a second adjective: ")
Adjective3 = raw_input("Enter a third adjective: ")

Verb1 = raw_input("Enter a verb: ")
Verb2 = raw_input("Enter a second verb: ")
Verb3 = raw_input("Enter a third verb: ")

Noun1 = raw_input("Enter a noun: ")
Noun2 = raw_input("Enter a noun: ")
Noun3 = raw_input("Enter a noun: ")
Noun4 = raw_input("Enter a noun: ")

animal = raw_input("Enter an animal: ")
food = raw_input("Enter a food: ")
fruit = raw_input("Enter a fruit: ")
number = raw_input("Enter a number: ")
superhero_name = raw_input("Enter a superhero_name")
country = raw_input("Enter a country")
dessert = raw_input("Enter a dessert")
year = raw_input("Enter a year")

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

print STORY % (Adjective1, name, Verb1, Adjective2, Noun1, Noun2, animal, food, Verb2, Noun3, fruit, Adjective3, name, Verb3, number, name , superhero_name, superhero_name, name, country, name, dessert, name, year, Noun4)

Terminal :

python Madlibs.py File "Madlibs.py", line 1 This program does the following ... Writes a Mad Libs story ^ SyntaxError: invalid syntax python Madlibs.py
File “Madlibs.py”, line 1
This program does the following … writes
a Mad Libs story
^
SyntaxError: invalid syntax
$

Why keep getting this invalid syntax error ?

1 Like

please edit your post and before and after your code please place three backticks --> ` <–

thank you!

if you want to put this in the code:

Author: Cai Gengyang

you need to make it into a comment or use it a print. You can’t just write text into the file, everything is syntax

1 Like

Yea, I made this line “Author: Cai Gengyang” into a comment — # Author: Cai Gengyang , but it still doesn’t work. I still get the same Syntax Error: invalid syntax error message …

Well, you made a topic with a question, which we answered. You can post an updated version of your code, do use markup:

I will see if i see another problem, otherwise you might be lucky and nick will have another look

1 Like

Have you commented all the text lines you want to print, just like @stetim94 mentioned ?

1 Like

Yea, i commented out the 1st and 2nd lines and managed to get the code to run / enter the inputs. However, I got a different error now — TypeError: not all arguments converted during string formatting on line 34. How can I solve this problem ? Thanks alot …

Madlibs.py

This program does the following … writes a Mad Libs story

Author: Cai Gengyang

print “Mad Libs is starting!”

name = raw_input("Enter a name: ")

Adjective1 = raw_input("Enter an adjective: ")
Adjective2 = raw_input("Enter a second adjective: ")
Adjective3 = raw_input("Enter a third adjective: ")

Verb1 = raw_input("Enter a verb: ")
Verb2 = raw_input("Enter a second verb: ")
Verb3 = raw_input("Enter a third verb: ")

Noun1 = raw_input("Enter a noun: ")
Noun2 = raw_input("Enter a noun: ")
Noun3 = raw_input("Enter a noun: ")
Noun4 = raw_input("Enter a noun: ")

animal = raw_input("Enter an animal: ")
food = raw_input("Enter a food: ")
fruit = raw_input("Enter a fruit: ")
number = raw_input("Enter a number: ")
superhero_name = raw_input("Enter a superhero_name: ")
country = raw_input("Enter a country: ")
dessert = raw_input("Enter a dessert: ")
year = raw_input("Enter a year: ")

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

print STORY % (Adjective1, name, Verb1, Adjective2, Noun1, Noun2, animal, food, Verb2, Noun3, fruit, Adjective3, name, Verb3, number, name , superhero_name, superhero_name, name, country, name, dessert, name, year, Noun4)

Terminal

python Madlibs.py Mad Libs is starting! Enter a name: Cai Gengyang Enter an adjective: small Enter a second adjective: big Enter a third adjective: large Enter a verb: hit Enter a second verb: ■■■■ Enter a third verb: run Enter a noun: anger Enter a noun: belief Enter a noun: hope Enter a noun: life Enter an animal: giraffe Enter a food: burger Enter a fruit: watermelon Enter a number: 5 Enter a superhero_name: superman Enter a country: singapore Enter a dessert: icekachang Enter a year: 1985 Traceback (most recent call last): File "Madlibs.py", line 34, in <module> print STORY % (Adjective1, name, Verb1, Adj ective2, Noun1, Noun2, animal, food, Verb2, Nou n3, fruit, Adjective3, name, Verb3, number, nam e , superhero_name, superhero_name, name, count ry, name, dessert, name, year, Noun4) TypeError: not all arguments converted during s tring formatting

https://www.codecademy.com/en/courses/python-ext/projects/madlibs

#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 %ss protesting to keep %s in stores. The crowd began to %s to the rythym of the %s, which made all of the %ss 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 % (adj_1,user_name,verb_1,adj_2,noun_1,noun_2,animal,food,verb_2,noun_3,fruit,adj_3,user_name,verb_3,number,user_name,hero,hero,user_name,country,user_name,dessert,user_name,year,noun_4)              

In all there are 25 parameters (some repeated values). Be sure to replace all the __ with %s.

I don’t know if you read the part on variable naming conventions, but the first thing I would recomment is to not use capital letters in the first character of a name. This is reserved for classes.

Still get error message though …

Input —

This program does the following … writes a Mad Libs story

Author: Cai Gengyang

print “Mad Libs is starting!”

name = raw%sinput("Enter a name: ")

adjective1 = raw%sinput("Enter an adjective: ")
adjective2 = raw%sinput("Enter a second adjective: ")
adjective3 = raw%sinput("Enter a third adjective: ")

verb1 = raw%sinput("Enter a verb: ")
verb2 = raw%sinput("Enter a second verb: ")
verb3 = raw%sinput("Enter a third verb: ")

noun1 = raw%sinput("Enter a noun: ")
noun2 = raw%sinput("Enter a noun: ")
noun3 = raw%sinput("Enter a noun: ")
noun4 = raw%sinput("Enter a noun: ")

animal = raw%sinput("Enter an animal: ")
food = raw%sinput("Enter a food: ")
fruit = raw%sinput("Enter a fruit: ")
number = raw%sinput("Enter a number: ")
superhero_name = raw%sinput("Enter a superhero_name: ")
country = raw%sinput("Enter a country: ")
dessert = raw%sinput("Enter a dessert: ")
year = raw%sinput("Enter a year: ")

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

print STORY % (Adjective1, name, Verb1, Adjective2, Noun1, Noun2, animal, food, Verb2, Noun3, fruit, Adjective3, name, Verb3, number, name , superhero_name, superhero_name, name, country, name, dessert, name, year, Noun4)

Output —

python Madlibs.py Mad Libs is starting! Traceback (most recent call last): File "Madlibs.py", line 7, in <module> name = raw%sinput("Enter a name: ") NameError: name 'raw' is not defined

There are 18 %s str values, where there should be 25. Note the seven _ characters. Change them to ‘%s’ and you have 25.

Global correction can have drawbacks. raw_input.