Python Development Course
Chapter
>
Level
Creating Lists
String Lists
Objective
Put food down and call the piglets over by putting their names on a list.
The piglets will probably be getting hungry soon, pick up the large bag of pig food and head for the two (2) dark X marks on the map. Use the place() function to pour the food from the bag for the piglets to eat, like this: player.place("food") .
After you’re done setting down the food on both dark X marks, head to the light X mark and call over the piglets so they can come eat. Their names are “Peggle” , “Peter”, “Piper” and “Pedro” . To make it easier to call them, put their names in a list, like this: names = ["Peggle", "Peter", "Piper", "Pedro"]
Use a for loop to call all the names of the piglets using the speak() function. Add the call message "Come here %s” . The %s allows you to add a string to the call, in this case a name.
names = ["Peggle", "Peter", "Piper", "Pedro"] for x in names: player.speak("Come here %s" % x)
In this code the player will be able to call out the names of all the piglets in order. The x in the for loop counts which cycle the loop is on as a result it goes through each name in the list in order until it reaches the end.