Level 3: While Loops - IncrediCoders/Python1 GitHub Wiki

GitHubAvatar copy

Annie Conda added this page on March 15, 2023


Here is a bonus article that will tell you more about While Loops!

At the most basic level, a counting loop is repeating the code that is indented below it. A coding example of this would be trying to move down the page with a sprite as you did in Level 1! First, you want to figure out what the command is which in this case is "move down 1 pixel." Then, you figure out how many pixels are between the sprite and the bottom of the page, then you write "repeat [number of times]." For example, if it is 50 movements, you would write "repeat [50]" and then on the next line (indented) you would write "move down 1 pixel."

Another type of loop is a conditional loop. This is similar to counting loops but instead of a repeat, there is a condition. Using the same "move down 1 pixel" example, for a conditional loop, "until" is a keyword. For this example, you would write "[until sprite reaches bottom]" and then on the next line (indented) write "move down 1 pixel." This means that the sprite will move down 1 pixel until it has reached the bottom of the page, and this loop will stop.

See more about these loops here: Explaining Loops

However, for coding there are actual code command words that correlate with the different loops described above.

The first of these is the while loop. This loop has the same function as the conditional loop described above. Using the same example, moving one pixel with your sprite until you reach the bottom, a while loop would look like this:

while (MY.y_position != 280)

{

MY.y_position += 1;

}

MY.y_position is just a placeholder representing where the sprite is vertically on the page, the != means not equal to 280 pixels which all together means when the sprites vertical position is not equal to 280 pixels (the bottom) do the following line of code indented below. Then on that line below, += means that you add 1 pixel to the variable representing the vertical position of the sprite until that variable's value is 280 pixels.

Here is a video with a lot more helpful information: What are Loops?

Next Steps

Next, you can take on the two extra challenges and a bonus challenge to add to your Classroom Quiz program and learn more! When you're done, you can move on to Level 4, the Space Wars game!

Take the Challenges!

  1. Challenge 1: Add a score to the game, so you can try to get all the questions right. When the game ends, display the score!

  2. Challenge 2: Make the text of the answer choices turn red and green to show which one is the correct answer, as a visual feedback effect.

  3. Bonus Challenge: First, you're going to load in a new quiz with more questions! Then, you're going to add in some of your own quiz questions! Finally, you're going to make your own quiz about whatever you want!

More Level 3 Resources

In addition to this Help page and the instructions for our Level 3 challenges, we also have Online Articles, a Learning Quiz, an Unplugged Activity, and a Rewards article:

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

  • Level 3: 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 3: 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 developer, and one person act as the variable where the developer chooses a noun for the variable to say in the story that they tell!

  • Level 3: Rewards - If you completed the Class Introductions project that we talked about, 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 Apple Award digital download, to show off your accomplishment!

Level 4

After you're completely done with Level 3 (did you do the challenges?), then it's time to move on to Level 4! While you read through Level 4 in your book, you can check out the resources from SideWinder, as she teaches you how to build the Space Wars game:

I hope you had fun learning about Classroom Quiz! This is something that is very important for all of the future levels! Enjoy!

-- Annie

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