Python Development Course
Chapter
>
Level
Creating your own Functions
Using lists in functions
Objective
Collect all vegetables in the field and use a custom function to pickle them.
Pickling is when you seal vegetables in a jar with vinegar and spices in order to give them flavor and preserve them for long periods of time. The field has several vegetables you can pickle using the press by the shed, collect all the vegetables and try it out!
First we would need to create a custom function named pickle_food that would allow us to pickle the vegetables using two (2) lists in order to put everything together.
def pickle_food(food,amount)
for x in range(3):
player.speak("You have pickled %d %s"%(amount[x], food[x]))
The custom function uses the lists as arguments, one for food and the other for amount . This means we need to create a list that stores the names of the food we wish to pickle and another list containing the amount of each food we wish to pickle. Itâs important to have a separate list for the amount as the number of vegetables you choose to pickle can vary.
Collect all the vegetables in the field and create a list named vegetables for the vegetable names and one called vegetable_number for the amount, like this: vegetables = [] , vegetable_number = []. Then append() the vegetables you've collected to the names list, their names being: "Cucumbers" , "Tomatoes" and "Radishes".
After populating the vegetables list, make variables for each vegetable and store the number youâve acquired of each, then place those variables on the vegetable_number list. For example: cucumbers = 1 , tomatoes = 2 , radishes = 3 , vegetables_number = [cucumbers, tomatoes, radishes].
Once you have everything, head to the X mark and use the custom function with the lists you created in order to pickle the veggies and complete the level.