Python Development Course
Chapter
>
Level
Conditions
Loop Conditions
Objective
Collect and dump all the weeds in the passages using loop conditions using no more than twelve (12) lines of code.
The path to the courtyard is full of weeds that need to be taken care of. You can automate this process by using loops and using the else statement to it’s fullest.
The passages around the courtyard is full of weeds, collect them and use the place() function to dump them in a container at the end of each passage. This should be easy with loops however, there are three (3) passages, two of these passages have nine (9) weeds, one has five (5).
Create a for loop with a range() of three (3) so the code inside the loop can encompass all three (3) passages.
for x in range(3): # Movement Code if x == 0: bag = 5 # Insert Place() code here player.turn_left() else: bag = 9 # Insert Place() code here player.turn_left()
There are five (5) weeds in the first passage and nine (9) in the other passages, use an if statement to check if the loop is in it’s first cycle by checking if x is equal == to 0. Remember the x variable is what checks what cycle the loop is in and it always starts at 0.
If the if statement returns true, create a variable named bag to store the five(5) weeds and use the place() function to dump them in a container, like this: player.place(bag). Use the else statement to run a variation of the same code but have nine (9) weeds in the bag if the loop is running anything other than the first cycle.
Complete the level by collecting all weeds and placing the correct number of them in each container using no more than twelve (12) lines of code.