This is an archive of the README file for Day 2 of the Fall 2013 tracks.
##MadLibs
Open the starter file: madlibs.py. Copy all the code into a new Python window.
If you try to run it, what happens?
Look at the code - you have set all the variables for the program to run. We can use raw_input
(from the last session) to ask the user for words:
adjective2 = raw_input("Enter another adjective: ")
adjective3 = raw_input("Enter yet another adjective: ")
adjective4 = raw_input("Enter a fourth adjective: ")
Notice that you can use commas to string things together in a print
statement. Python automatically adds a space when you do something like "Hello,", name
.
You can also notice that IDLE automatically adds colors to code. Built-in functions like raw_input
are purple, keywords are orange, and strings are green. Comments (that Python ignores) are red, and most other things are black. The colors don't matter to Python, but they're useful to help you understand your code.
If you run the program, Python takes all our variables and sticks them into our sentences. Try running the program multiple times and see what sentences you can create!
##Silly Sentence Generator
Our next project is kind of like MadLibs, but instead of asking the user for words, the computer is going to pick words itself.
Open the starter file: [silly sentence generator.py](silly sentence generator.py). Copy all the code into a new Python window.
###Let's look at the code:
####Lists
Up until now, we've stored things in one variable at a time. A list is a way to collect multiple values in one variable. Let's make a list:
>>> grocery_list = ["apples","bananas","a pet chicken"]
We can look at specific items in the list with an index, and indexes start from zero.
>>> print grocery_list[0]
apples
####Random
We can use the random module to create random numbers:
import random
random.randint(0,5)
A module is something extra in Python that lets you go beyond what Python lets you do by default.
####The random_word
Function
The random_word
function picks a random index from 0 to the last item in the list. Then, it finds the word with that index, and hands it back:
#this function is given a list of words and selects one at random
def random_word(list_of_words):
number_of_words = len(list_of_words) #get the length of the words list
random_word_number = random.randint(0,number_of_words - 1) #pick a random number up to the end of the list
selected_word = list_of_words[random_word_number] #select the word at the number spot
return selected_word #hand it back
Functions in Python let you take a piece of code and use it over and over again.
#####Functions
We can use the def
keyword to define a function. Functions let you send something to the function, and the function sends you something back. You can re-use functions by sending them different data, and they'll send you different data back.
In the statement random_word(verb_list)
, verb_list
is what we're sending to the function. The function doesn't care that it's the verb list, so we can give it any list of words.
###Add our own code!
####Create your own noun and adjective lists
Look at how the verb list was done. We can make our own lists of adjectives and nouns, and maybe add some more verbs for variety.
####Use the function to select random words from each list
You will need 5 variables: adjective1, noun1, verb, adjective2, and noun2. Remember, we can re-use functions by passing them different information, and they'll send different information back.
####Print the result in a sentence.
Be sure to insert the word "the" in the right places. What sentences can you make?
####random.choice
We just wrote a function to pick a random word from a list of words. However, the random
module already gives us a way to do this. We can use random.choice
to write a new version of random_word
:
def random_word2(list_of_words):
return random.choice(list_of_words)
We've just shortened a 4-line function to a 1-line function. When you have a 1-line function, you have to think, "Do I really need that function?" In this case, it's kind of OK, because we arrived at that 1-line function by a process of iteration.
####Making more silly sentences Can you change the program to print 10 silly sentences instead of 1? Look back at the notes from last week if you need to remember how loops work.
####Possible extensions
- Improve your word list. If you like Minecraft, for example, add some Minecraft terms!
- Improve the sentences' grammar
- Make the sentences longer, with more word types
- Just see how creative you can get with your sentences!
##Challenge Problem
Here is a bonus MadLib that you can turn into a program:
print "Make Me A Video Game MadLib!"
print "I, the", adj1, "and", adj2, name1 + ", has", past_tense_verb, name2 + "'s sister and plans"
print "to steal her", adj3, noun + "! What are a", animal1, "and backpacking", animal2
print "to do? Before you can help", name3, "you'll have to collect the", adj4
print plural_noun1,"and",adj5, plural_noun2, "that open up the", number1, "worlds connected to"
print name4 + "'s Lair. There are", number2, plural_noun3, "and", number3, plural_noun4, "in the game,"
print "along with hundreds of other goodies for you to find."