You Need To Get A Pet!
Back to index
There are lots of animals that make good pets, but not everyone likes
animals enough to keep them at their domicile. In fact, many of us prefer
plants! But, be that as it may, this lesson featues a fairly long, although
not quite extensive, list of pets. It also introduces a concept known as
iteration in the form of a for-loop. Get the following code up and running
and analyze it in an attempt to understand how it works.
#!/usr/bin/python
stuff=["cat", "dog", "rat", "fish", "hamster", "mouse",
"bird", "goat", "duck", "turtle", "snake"]
ITEMS = []
for x in stuff:
item = raw_input("Do you have a " + x + "? ")
if item=='y':
ITEMS.append(x)
if ITEMS:
print "YOUR PETS: "
print ITEMS
else:
print "You need to get a pet!"
The variable called stuff contains a list of several common pets. The
variable called ITEMS begins its life as an empty list. Notice that both
stuff and ITEMS utilize square brackets. The difference being that stuff has
stuff in between the brackets and ITEMS doesn't. However, by the end of the
execution of the program it is possible for ITEMS to contain all the items
listed in stuff, depending on the behavior of the person using the program.
The first thing the user sees after launching this program is a question.
That question is "Do you have a cat?" Although the program provides no
instructions, if the user enters a "y", then cat gets added to the empty
list known as ITEMS. The user is then interrogated with a long list of
questions pertaining to the animals listed in stuff. Each time the user
enters a "y" (and nothing else), the current item is added to ITEMS. The
append function adds items to a list. (Notice that double equal signs are
used to compare values, whereas a single equal sign is used to store a value
into a variable.)
Finally the program ends with an IF-ELSE construct. If ITEMS is empty, then
the user receives the message "You need to get a pet!" Otherwise a list of
pets the user answered "y" to is presented.
It's as simple as that!
ASSIGNMENT:
You will rewrite this program so that the basic idea remains the same, but
the approach is altered somewhat. Here's a list of changes that must be made
to successfully complete this assignment:
- Provide the user with adequate directions so that the user knows how to
use the altered program.
- Begin the program by asking the user if she/he has any pets. If the user
indicates that she/he has no pets, then end the program with some sort of
friendly recommendation pertaining to pets.
- If the users indicates that she/he has pets, then ask the user to enter
one pet at a time when prompted.
- Provide prompts to allow for the entry of pets and store these items in
a list variable.
- Provide a way for the user to indicate when she/he is done entering more
pets. For instance, entering the word "done" is one way that you might
arrange for the user to indicate that she/he is done.
- Report back to the user the list of pets she/he entered.
Obviously this method does not prevent the user from entering nonsense, but
it does allow the user to enter a pet which the programmer did not
anticipate. After all, there are lots of animals that could conceivably make
good pets and it would be an unpleasant experience to sit through a
question/answer session in which all the possibilities are listed.
HINT: A while loop is probably the best way to deal with input. Try this:
while input != "done":
input = raw_input("List another pet you own: ")
my_pets.append(input)
|