Intro to Programming Using Robots
Lab: Feb 19, 2009 (Due Feb 24th)
VERY IMPORTANT!!!!!
You must read this lab very
carefully. Do every step -- don't skip anything!!!!
Remember that askQuestion
won't work unless you do from myro import *
Part 1: What you should already have
- If you didn't do this yet, do this first.
- If you couldn't do this, ask for help!!!
For today, you should have already written a mad libs program with the
following specification:
- Before you start programming, you should write a 3 line story, at
the end of each line, the story is missing a noun. Here's
an example of a really lousy story
- Now put that story into three variables. e.g., you might say:
line1 = "Once upon a time there was a"
- Next make a list of 10 nouns. Note that you can hit enter in between the items in the list
(but not anywhere else) so it doesn't zoom off the edge of the screen.
- Next, use askQuestion to ask the user for 3 nouns that they like
(make sure you did "from myro import *" first or askQuestion won't work)
- Now, have the computer speak out the story by alternating reading
out the lines of the story and the nouns.
Part 2: Practice playing with lists
Recall that we can look at individual items in a list using the
position number (as long as we count like geeks). So if I have a list
like this:
colors = ["red", "green", "blue", "yellow"]
Then if I say:
print colors[2]
The word "blue" will get printed (because we start counting at 0)
Write a function with your own list of four colors in it (be sure to
call it colors). Then add the following piece of code:
for giraffe in range(4):
print giraffe, "--", colors[giraffe]
Can you see what's going on? If not, talk to your neighbor, or ask the
instructor.
Part 3: More playing with lists
Now make a new function called part3. In this function you should
create 3 lists of strings.
Note that you can hit enter in between the items in the list
(but not anywhere else) so it doesn't zoom off the edge of the screen
and wrap around when you print.
- The first, called colors, should be a list of 10 different colors
(pick your favorite)
- The second, called adjectives, should be a list of 10 different
adjectives that aren't colors (words that describe nouns, like "big",
or "scary", etc.)
- The third, called nouns, should be a list of 10 different nouns
(person, place, or thing, e.g. "refrigerator")
Now, using the techniques from part 2, use a
for i in range(10)
command to print out 10 adjective, color, noun sets with the word "The"
at the front.
For example, if the start of your colors list looks like this:
colors = ["red", "green", "blue",
And the start of your adjectives like this:
adjectives = ["big", "small", "mean",
And the start of your nouns looks like this:
nouns = ["cat", "dog", "plant",
Then your program is going to start by printing
The big red cat
The small green dog
The mean blue plant
Part 4: Review: Making lists
Recall that we can add things to a list using the append command, and
that []
is the empty list.
You can also create a new list by just saying what goes in it, and you
can put variables in there too
Copy the following code and run it. Make sure you understand what
happens on each line, if you don't, ask the instructor:
def part4():
first_list = []
print "Here's the first list:", first_list
#print by itself like on the next line prints a blank line
print
first_list.append("Hello")
print "Here's the first list now:", first_list
print
foo = "Goodbye"
first_list.append(foo)
print "And here it is again:", first_list
print
print "Now I'm going to make a list using strings and variables"
first = "Rowan"
second = "President Farish"
another_list = [second, "has been at", first, "for around", 10, "years"]
print another_list
Part 5: Now you make some lists
Write a new function called part5. Copy and paste your lists of 10
colors, adjectives, and nouns into this code. Use askQuestion to ask
the user for their favorite color. Then use it again to ask them for
their favorite adjective. And again for their favorite noun. Now make a
list that has these three words in it and print it.
So, for example, if their favorite color is red, their favorite
noun is plant, and their favorite adjective is "big", then you might
print something like:
my list is: ['red', 'plant', 'big']
Part 6: Back to Mad Libs: Putting it all together
Now take your 3 line story, and turn it into a list that has 3 strings.
For example, my story turns into:
my_story = ["Once upon a time there was a ",
"he really liked to eat"]
(remember you can hit enter in between the items in the list
(but not anywhere else) so it doesn't zoom off the edge of the screen
and wrap around when you print.)
Then use your list of nouns and ask the user 3 times for a noun. Turn
the three answers that they give you into another list called user_nouns
At this point you should have a list called my_story
and another list called user_nouns.
Now use a for command with range 3 to do a computer.speak command to
alternate between saying lines in your story and nouns. So the computer
should tell your story.
Part 7: Getting ready for next time: part a: story
If you've finished all of this, it's time to write a *cool*
story.
Create a text file by saying File->new
window in python and then do a File->save as
and name it something.txt (i.e. put .txt at
the end not .py)
Your "story" file should have 10 lines in it. It should tell a story
that is missing 10 words. Each "missing" word should go at the end of
one of the lines in the story. You should leave out 4 nouns, 3 verbs, 2
adjectives, and 1 place.
Here's an example of a 4 line story, that's missing 4 words (one at the
end of each line) to show you what I mean:
Once upon a time there was a
He liked to go to
and
all day long with his friend the
The above story is missing a noun on the first line, a place on the
second, a verb on the third, and another noun on the last line.
Here is an example of a more interesting
story. Note that your story isn't limited to 10 sentences, you can
put as many sentences as you want on a line, just end with a missing
word like I do here. (By the way, I got this story from
http://www.magickeys.com/books/alien/index.html but I expect you to
write an interesting story yourself!
Part 8: Getting ready for next time: part b: parts of speech
create 4 more new files for parts-of-speech text files
- A file with 15 nouns in it, each on their own line
- A file with 15 places in it, each on their own line
- A file with 15 verbs in it, each on their own line
- A file with 15 adjectives in it, each on their own line