Level 1: Challenge 2 - IncrediCoders/Python1 GitHub Wiki

GitHubAvatar G_Look down 1

Grafika Turtle added this page on March 14, 2023


Let's begin your second challenge for level 1!

In this challenge, your goal is to go back and forth between Grafika's house and the school, a total of five times.

Explaining the Code

On Line 2, from init import * imports in the code from the init.py file. The code in that file grabs the files from your folder. In other levels, the import files will have more lines of code in them.

On Line 3, import turtle brings in the turtle module, which includes a lot of code you can use (called methods). For example, you can tell the turtle which direction to go in (and how far to go), you can set the color and shape of the turtle, etc.

On Line 6, SCREEN = turtle.Screen() creates a new window for your turtle program. In this case, SCREEN is a variable that we are initializing. We put the function turtle.Screen() into the variable SCREEN. Let's review what a variable is.

Variable

A variable is a storage location (kind of like a drawer) that's given a specific name (such as SCREEN, in this example). A variable contains a known or unknown quantity of information, which is referred to as a value. For example, it could be a number or a string, which is a bunch of text in quotes (like, "Grafika is great at explaining things"). Or it could be other code, like another variable or a function (which is how we're using it).

Now let me explain what we mean when we say that we're initializing a variable.

Initialize

To initialize means to set the value of a variable to something that starts the operation, in case we're constructing the loop. For example, in a loop we use a variable to track which pass of the loop we're on (such as the third time through).

On Line 7, SCREEN.title("Mapster Spacebook") sets the title name ("Mapster Spacebook") for the window that we created in the last line of code.

On Line 8, SCREEN.setup(1280, 800) sets the size of the window that we made in the code from Line 6. 1280 x 800 represent the pixels that determine the size of the window.

On Line 9, SCREEN.bgpic(get_file("Assets/Background.gif")) sets up our background image on the window. This image shows the map of the roads that you'll use, to move the turtle back and forth, from my house to the school (we'll be making five trips).

To review, here are Lines 6-9 again, which set up the window, the title of the window, set the size of the window, and put the background image in the window:

SCREEN = turtle.Screen()

SCREEN.title("Mapster Spacebook")

SCREEN.setup(1280, 800)

SCREEN.bgpic(get_file("Assets/Background.gif"))

Moving the Turtle

On Line 12, turtle.shape("turtle") creates a new turtle character using the code from the imported turtle class.

On Line 13, turtle.color("green") changes the color of the turtle character that you created in the code from Line 12. We change it to green using the code that we got from the imported turtle class.

On Line 16, turtle.penup() lifts the pen off the window so when it is moved, it does not create any lines on the window.

On Line 17, turtle.setx(460) sets the x position of the turtle character that we created in the code from Line 12. The position is set to 460 pixels (to the right of the middle of the window).

On Line 18, turtle.sety(-275) sets the y position of the turtle character that we created in the code from Line 12. The position is set to -275 pixels (below the middle of the window). Together, Lines 17 and 18 place the turtle at my turtle house!

On Line 19, turtle.pendown() puts the pen back down on the window, so that when it is moved, it will create a straight line from its current location to its next location.

To review, here are Lines 12-19 that edit and move the sprite:

turtle.shape("turtle")

turtle.color("green")

turtle.penup()

turtle.setx(460)

turtle.sety(-275)

turtle.pendown()

Write Your Own Code

On Line 21, you'll see the code #TODO: Write a loop to go back and forth 5 times. You're going to add a loop that runs the same code five times. This line does not need to be uncommented because it is the instructions, not a part of the code. So, what's this loop I'm talking about?

For Loop

A For loop is a group of code that will repeat as many times as possible as long as the condition is true. For example, let's say your condition is that the loop will run until your variable equals 10. If your variable is initially set to equal zero, and you add one to your variable each time the loop runs, then your loop runs 10 times. This saves you from writing those lines of code more than one time.

On Line 22, you'll see #To learn more about loops, follow the instructions on the wiki page. You're here! To learn more about Python For Loops, see this online articles:

On Line 23, you're writing your first line of code! Since you are making a loop, you want to set the conditional statement first. Let me describe what a conditional statement is:

Conditional Statement

Conditional statements check to see if your condition is true, such as for jellybean in range(5), which assigns the number 0 to the jellybean variable the first time, and then it adds 1 to the variable each time the loop runs. You exit the loop when jellybean equals 5.

Inside of each conditional statement, there has to be a condition that can be either true or false (such as whether jellybean is equal to 5). I'll explain what a condition is.

Condition

A condition is a statement that is created by the programmer, which evaluates whether an action is true or false. For example, when we run an if condition, we might check if tacos > 5.

You'll want to start your conditional statement (on Line 23), by typing the word for. This is a For loop. Then add a space (stay on the same line of code).

Next, you'll want to create a new variable, like I described earlier. It is helpful to name a variable based on its purpose. In this case, type in trip_number as your variable name.

Then add a space and type in range (5):

The number 5 is for how many times you're running your loop. In this case, the turtle is going back and forth, to and from school, five times. Make sure you end your For loop statement with a colon!

Next, let me describe what the in and range functions mean, so you can use it for this challenge:

In Range

A variable that you create, such as "trip_number" assumes that you start at 0 and are incrementing by one. This is because there is only one argument. Then, each time the loop runs, your variable will increase by one. In this case, you want to make 5 trips, so the way we write this in code is in range(5).

Again, make sure you end the line with a colon. When you put all of these together, you'll have written for trip_number in range(5): on Line 23.

Line 24 says, #TODO: In your loop, write the code to go to the school and then to get back, and Line 25 says, #(You can use your code you wrote from the instructions in the book!).

Next, you're going to write the code for Lines 26-34. This code will take the turtle from Grafika's turtle house (on the lower right corner of the background map) to IncrediCoders Academy, in the upper left corner of the map. As the comment says, you can use the instructions or code from the book to fill in this section.

For the code on lines 26-47, there are three important things to remember:

  1. Make sure you're properly indented, before writing your code.
  2. There are two commands for each movement, one to turn the turtle and one to move it forward.
  3. We'll leave blank lines on Lines 30, 35, and 41. This helps make it easier to understand what the code is doing.

On Line 26, start the first movement by giving your first command to the turtle. Looking at the map, you see that the turtle needs to turn left 180 degrees, to face to your left. As a result, type turtle.left(180) on Line 26.

On Line 27, continue the first movement by giving the second command to the turtle. Looking at the map to get to the next turn in the road, the turtle needs to move forward 510 pixels. Type turtle.forward(510) on Line 27.

On Line 28, you will turn the turtle again. When you look at the map, you'll see that the turtle character needs to turn 90 degrees to the right. Type turtle.right(90) on Line 28.

On Line 29, you'll you move the turtle forward 220 pixels to the next turn in the road. Type turtle.forward(220) on Line 29.

You'll leave Line 30 blank.

On Line 31, you'll turn the turtle to the left 90 degrees. Type turtle.left(90) on Line 31.

On Line 32, you'll move the turtle forward 490 pixels. Type turtle.forward(490) on Line 32.

On Line 33, to point the turtle so that it's heading towards the school, you'll turn it to the right 90 degrees. Type turtle.right(90) on Line 33.

And then on Line 34, now that the turtle is facing the direction of the school, you'll move the turtle forward 250 pixels. Type turtle.forward(250) on Line 34.

Leave Line 35 blank, so that it's easier to understand what the code is doing. You've completed the turtle's trip to the school!

Since you have reached the school! The comment on Line 36 line lets you know that now you need to write the code to go back to Syntax's and my turtle house in the lower-right corner of the map.

Next, you'll start the code for the turtle's journey from the school back to the turtle house. This movement will have eight commands before reaching the turtle house, which you'll write on Lines 37-45 with a blank space on Line 41.

On Line 37, the first command that you'll write is to turn the turtle 180 degrees to the left. This turns the turtle around, so that it's facing down on the map. Type turtle.left(180) on Line 37. You should be indented at the same level as the comment on Line 36.

On Line 38, you'll move the turtle forward 250 pixels, as it leaves the school. Type turtle.forward(250) on Line 38.

On Line 39, you'll turn the turtle to the left 90 degrees. Type turtle.left(90) on Line 39.

On Line 40, you'll move the turtle forward 490 pixels. Type turtle.forward(490) on Line 40.

Leave Line 41 blank.

On Line 42, you'll turn the turtle to the right 90 degrees. Type turtle.right(90) on Line 42. Again, you'll want to make sure you're indented at the same level as Lines 24-40.

On Line 43, you'll move the turtle forward 220 pixels. Type turtle.forward(220) on Line 43.

On Line 44, you'll turn the turtle to the left 90 degrees, to face my turtle house! Type turtle.left(90) on Line 44.

On Line 45, you'll move the turtle forward 510 pixels so it can get back to the turtle house! Type turtle.forward(510) on Line 45.

On Line 46, you'll see the comment #End the loop here. The loop has ended, and you're finished writing the code for this challenge! You should see Lines 24-45 all indented to the right one level from the for loop on Line 23. Since Lines 26-45 take the turtle to the school and back, that repeats five times in the loop!

I left Line 47 blank to visually separate the loop from the last few lines of code.

The comment on Line 48, #This line stops the window from closing once we make it to the end, explains that the function on Line 49 makes sure the window doesn't close. That code is turtle.done() on Line 49.

Run the code and see if it works. If you are running into issues, make sure that Lines 26-45 are all tabbed (indented to the right of Line 23)!

Congratulations! You have made it to the house and back five times!

Next Steps

More Level 1 Resources

In addition to this page, we also have Level 1 Help (for the main game, in the book), the Level 1 Challenge 1 instructions, Online Articles, a Learning Quiz, an Unplugged Activity, and a Rewards article:

  • Level 1: Help - This page helps you complete the instructions in the book, in case you get stuck.

  • Level 1: Challenge 1 - On this page, I show you how to map a different route to our school, the IncrediCoders Academy. This time, we're going to cross the bridge, over the lake.

  • Level 1: Online Articles - I made you a list of different web pages I found, which will help you learn more about creating Turtle Graphics.

  • Level 1: Learning Quiz - I wrote some questions in case you want to quiz yourself about what you learned. Or you can teach others and quiz them!

  • Level 1: Unplugged Activity - I wrote this page with more details than what you saw in the book. In this game, you'll have one person act as the Turtle, and one person is the Programmer!

  • Level 1: Rewards - If you completed the Turtle Map project that we talked about in the car, then I set up this page to act as a reward. You can see some illustrations of me and learn more about who I am! You'll also find the Turtle Award digital download, to show off your accomplishment!

Level 2

After you're completely done with Level 1 (did you do the Challenge 1?), then it's time to move on to Level 2! While you read through Level 2 in your book, you can check out the resources from Mrs. Scratcher, as she teaches you how to build the Class Discussion program shown here:

⚠️ **GitHub.com Fallback** ⚠️