Long before the War of the Magi, the evergreen floating islands were considered a monument to nature's beauty. But the islands changed when the evil warlock Urian engulfed the whole forest in a magical toxic haze.
Help Archie and Feather collect the sachets of pixie dust to unlock the mysteries of the jungle and restore the islands to their original beauty. Make sure you stay on the path and watch out for the carnivorous plants!
Use the forward()
command to reach the pixie dust.
Each time you use this function, the player moves one block forward.
How many forward()
commands do you need to reach the goal? Press run when you're ready to try out your solution.
Don't worry if don't you reach the goal on the first try, just try again!
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
When you're using programming languages like Python, the details really matter. If you misspell one word, or if you forget even a little bit of punctuation, your code won't run!
If you get an error, check your code closely for errors.
To move the character one step forward, you need to type the forward()
command in the coding area.
Write the forward()
command once more. Don't forget the parentheses!
Each time you use this function, put it on on a new line.
In the last programming puzzle, you wrote code for a computer to run. But humans need to read code too! We can add descriptions to our code using comments. This makes reading code a lot easier. Comments are also used to ignore some of your code to test things out or if you have code that you don't want to run at the moment.
Programmers often work together on teams, editing a single base of code, and use comments to describe their code to one another. If you're writing code with others, or want feedback from your teachers, you'll want to use comments too, so they can understand what you are trying to do.
Use comments whenever you're writing your own original Python code, too. They're also handy to remind yourself of what you were writing!
In Python, you type the pound character #
to start a new comment. Any text
that you write after that will be documentation for your code. In other words,
it won't affect how your program runs. Here's an example.
Click on the run button to see the output.
Notice the #
character can appear after your code. All the text that appears
after that #
won't affect the commands for the forward, turn right, forward
sequence.
The comment symbol can also be used to make lines of code not run. Programmers call this commenting out their code. You can experiment with code without deleting it in this way.
In this example, we use two print functions, but only one is executed. Which statement do you think will be skipped?
Click on the run button to see this in action.
Notice how only Feather
is printed. The first line is commented out, so it is not executed.
Great job! Some code has been written already to solve this next puzzle.
Just one problem, the solution has way too many forward()
commands. Comment
out the extra commands and get Feather to the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
When you're using programming languages like Python, the details really matter. If you misspell one word, or if you forget even a little bit of punctuation, your code won't run!
If you get an error, read your code closely for errors. Make sure you don't forget the parentheses.
To comment out a line of code, type #
at the beginning of the line.
Navigate the corner using the turn_right()
command and get that pixie dust!
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
turn_right() |
Turns the character to the right. |
turn_right()
command will turn the character to the right.Help Feather make a U-turn using the turn_right()
command. Make sure to grab that pixie dust!
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
turn_right() |
Turns the character to the right. |
forward()
multiple times and then turn_right()
once she reaches the corner.Enemy plant spotted! It looks hungry. . . Destroy the carnivorous plant before getting too close.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
fire() |
Launches an arrow that travels up to 5 steps ahead in the direction the character is facing. |
Fire at the carnivorous plant in the way using the fire()
command to get rid of it.
Each fire()
command launches an arrow that can travel up to 5 steps ahead in the direction the character is facing.
Help Feather destroy the carnivorous plants and reach the pixie dust. Feather
will climb the hills automatically—without using a forward()
command.
If you go too far, try experimenting with the number of forward()
commands.
You only need one for each tile of the puzzle.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
fire() |
Launches an arrow that travels up to 5 steps ahead in the direction the character is facing. |
Get rid of the plants by firing an arrow at them using the fire()
command.
Each fire()
command launches an arrow that can travel up to 5 steps ahead in the direction the character is facing.
Get close before taking your shot at the second carnivorous plant. You must be at the same elevation for the arrow to hit your target
Use the commands to help Archie reach the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
turn_left() |
Turns the character to the left. |
turn_left()
command when Archie reaches the corners.Sometimes you need to give the same commands over and over again. How many forward()
commands do you need to get Feather to the pixie dust?
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
In the last puzzle, you had to write the forward()
command over and over again.
That was kind of annoying, wasn't it? There's an easier way to repeat code!
We'll use something called loops to run the same code many times. To start things off, let's learn about a kind of loop called the for loop.
The for loop executes the same code a certain number of times. They are useful when you need to count (e.g. count from 0 to 9).
Here's what a for loop looks like in Python code.
You can customize your for loop by adding more information to range()
. The range specifier can receive up to 3 parameters, like this:
Increment describes how much to change the value of your counter with each loop. For example, if you specified an increment of 2
, you could print all even numbers. You could count backwards with an increment of -1
.
Start describes which value for your counter variable to start with.
If you don't set custom values for them, your for loop will use default values. By default, increment is set to 1 and start is set to 0.
Let's count down from 9 to 0. Notice in the code how we're using the extra parameters in range()
.
Click on the run button to see the output.
Did you notice that you used spaces in your for loops, for the commands you wanted to repeat? Programmers call this use of "white space" indentation.
When you're programming in Python, indentation is really important. If you don't indent your code properly, it will break or behave in unexpected ways.
Programmers use indentation to show the structure of their code and make the code more readable. You'll want to use 4 spaces to indent your code. Here's an example.
Click on the run button to see the output.
Notice how :
(a colon) ends the line of code right before the indentation starts. The code inside the for loop is then indented by 4 spaces.
After the loop, you stop indenting and start coding from the beginning of the line again.
Indenting your code also makes the code much easier to read and debug.
The command (or commands) you want to repeat must be indented. Make sure you fix any other syntax issues as well!
There is an easier way to do the same things over and over again! Use a for loop to get to the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
fire() |
Launches an arrow that travels up to 5 steps ahead in the direction the character is facing. |
Watch out for the carnivorous plants. Destroy them before going forward to collect the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
fire() |
Launches an arrow in the direction the character is facing. The arrow will stop once it hits a plant or travels up to 5 spaces. |
Move in a zig-zag pattern and collect the pixie dust. How many zig-zag patterns does it take?
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
turn_right() |
Turns the character to the right. |
turn_left() |
Turns the character to the left. |
fire() |
Launches an arrow that travels up to 5 steps ahead in the direction the character is facing. |
Uh oh! There is a gap in the path. Jump over the gap to get to the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
jump() |
Makes the character jump over a gap and land on the other side. |
jump()
command allows you to jump over the gap and land on the other side.Find the pattern and get Archie to the pixie dust. Watch out for gaps and carnivorous plants in your path!
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
fire() |
Launches an arrow that travels up to 5 steps ahead in the direction the character is facing. |
jump() |
Makes the character jump over a gap and land on the other side. |
This one has a slightly different pattern. Do you see it? Help Feather get to the pixie dust using a for loop.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
fire() |
Launches an arrow that travels up to 5 steps ahead in the direction the character is facing. |
jump() |
Makes the character jump over a gap and land on the other side. |
Jump the gap and get to the pixie dust. How many loops do you need?
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
fire() |
Launches an arrow that travels up to 5 steps ahead in the direction the character is facing. |
jump() |
Makes the character jump over a gap and land on the other side. |
turn_left() |
Turns the character to the left. |
turn_right() |
Turns the character to the right. |
You can write a program so that it performs different tasks based on a condition. In this way, you can have your program "make decisions" all on its own. Conditionals are used for this purpose.
An if statement executes a segment of code only if a condition is True. Otherwise your program won't execute that code.
The if statement is a very common kind of conditional, so let's take a closer look.
An if statement has the following syntax:
If the condition isn't met (that is, if it is False
), your program just skips right over any commands in the if statement.
A condition is what determines if the code inside the if statement will be executed.
For example, a condition might be i < 10
.
The code inside the if statement will be executed if the value of i
is less than 10
.
Try changing the values in the condition below. Click on the run button to see the output.
You can also write an if statement using a condition that uses functions that return True or False. Let's consider a function called has_path_right()
that returns True or False, depending on whether the path branches to the right.
Here's how we might write that out:
Use conditional logic to check for a path to the left and turn Feather to get to the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
turn_left() |
Turns the character to the left. |
has_path_left() |
Returns true if there is a path to the left. Otherwise, it returns false. |
fire() |
Launches an arrow that travels up to 5 steps ahead in the direction the character is facing. |
has_path_left()
command before turning the character to the left.Use conditional logic to check for a path to the right and turn Archie to get to the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
turn_right() |
Turns the character to the right. |
has_path_right() |
Returns true if there is a path to the right. Otherwise, it returns false. |
has_path_right()
command before turning the character to the right.Use conditional logic to make turns and help get Feather to the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
turn_right() |
Turns the character to the right. |
turn_left() |
Turns the character to the left. |
has_path_right() |
Returns true if there is a path to the right. Otherwise, it returns false. |
has_path_left() |
Returns true if there is a path to the left. Otherwise, it returns false. |
Check for a path left using the has_path_left()
command before turning the character to the left.
Check for a path right using the has_path_right()
command before turning the character to the right.
We used a for loop to execute the same code for a number of times. But we have other conditional loops in Python, too.
One such conditional loop is a while loop. A while loop executes the same code again and again—as long as the specified condition is true.
The while loop has the following syntax:
The condition determines how long the loop will continue to execute. For example, you might use a condition like i < 10
. This says keep those commands in the while loop running, as long as the value of i
is less than 10.
Let's use a while loop to count from 0 to 9. You need to initialize the variable i = 0
outside the while loop and increment the variable using the line i +=1
at the bottom of the loop.
The condition in this case is i < 10
.
Click on the run button to see it in action.
Now let's count down from 3 to 1, then have our code print something else once the while loop ends.
We need to decrement the variable i
at the bottom of the while loop using the line i-=1
. Notice after the while block ends, there's another print()
.
Click on the run button to see it in action.
Add your own while loop to print all odd numbers from 0 to 20 in ascending order. Try it below.
Use the while
loop to help Archie get the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
turn_left() |
Turns the character to the left. |
fire() |
Launches an arrow that travels 5 blocks in the direction the character is facing. |
enemy_in_sight() |
Returns true if an enemy is within a distance of 5 of the character's line of sight. |
reached_goal() |
Returns true if the character has reached the goal. Otherwise, it returns false. |
has_path_ahead() |
Returns true if there is a path in front. Otherwise, it returns false. |
Use the reached_goal()
command to check if Archie has reached the pixie dust.
Execute the loop as long the goal is not reached with the not
keyword.
Destroy the carnivorous plant using the fire()
command.
Use the enemy_in_sight()
command to check if there is an enemy (carnivorous plant) within 5 steps in front of the character.
Use the has_path_ahead()
command to check if there is a path in front of the character.
Watch out for the hazards in your path to the pixie dust.
Command | Description |
---|---|
forward() |
Moves the character one step forward. |
turn_left() |
Turns the character to the left. |
fire() |
Launches an arrow that travels 5 blocks in the direction the character is facing. |
jump() |
Makes the character jump over a gap and land on the other side. |
is_gap_ahead() |
Returns true if there is a jumpable gap ahead. |
enemy_in_sight() |
Returns true if an enemy is within a distance of 5 of the character's line of sight. |
reached_goal() |
Returns true if the character has reached the goal. Otherwise, it returns false. |
has_path_left() |
Returns true if there is a path to the left. Otherwise, it returns false. |
is_gap_ahead()
command to check if there is a gap in front of the player.