Level 3: Help - IncrediCoders/Python1 GitHub Wiki

GitHubAvatar copy2

Annie Conda added this page on March 15, 2023


Hello! This page will help you when you get stuck with the instructions I gave you on programming the Classroom Quiz game. Please make sure you go through the notes in the book first.

You'll want to make sure that your Level 3 files are loaded in Visual Studio Code. If you have any problems, please read the instructions at Load the IncrediCoders Files.

Here are the different sections on this page to help you out with your Classroom Quiz game:

  1. Play the Game - Run the complete Classroom Quiz game, to see what the final version looks like (after you go through the instructions).
  2. Glossary of Terms - This list goes through the instructions I sent you, and it explains every term and idea in depth, one topic at a time, as I bring it up in the book!
  3. Advice on Writing the Code - Eventually, in the book, I ask you to write your own code. Still stuck? Well, here are some hints on how to write the code!
  4. The Final Code - After you're done, it might not work the way you expected, or it might not work the same as our version (see Play the Game). Check out my code to see what you missed! IMPORTANT: Please don't cheat yourself! Finish the game first!

1. Play the Game

In this Classroom Quiz game, you can run the game to see what the finished game looks like before you build it (or while you're building it). You can see the full instructions on how to download and run the game at Try the IncrediCoders Games.

In this game, we will create a quiz for our class (called Classroom Quiz) to examine how well you know your classmates (the characters). The quiz includes an intro page, question page, and an end page. In the question page, we will show the question and its three answer choices for the player to choose from. All the questions are about information on the characters and there is only one correct answer for each question.

You can run the program to see what the finished game looks like before you build it (or while you're building it). You can see the full instructions on how to download and run the program at Try the IncrediCoders Games.

Here are the downloads to run the Level 3 program – Classroom Quiz:

Here are the instructions on how to play the Level 3 game: Level 3 Instructions

2. Glossary of Terms

  1. ClassroomQuiz.py: This is the file that you'll use as your template. You should have downloaded the file and loaded it into Visual Studio Code. Here is the file on GitHub: ClassroomQuiz.py
  2. init.py: This is the file that contains the settings of the layout and format for the game, and other functions for more operations to be used in ClassroomQuiz.py.
  3. Trivia.txt: This is the file that contains multiple sets of questions and their three answer choices. Every four lines contain a question, its correct answer, and two wrong answers.
  4. List: This is a Python data structure that stores a collection of values, like numbers, words or variables. For example, the TRIVIA list below is storing the text (strings).
  5. TRIVIA list: This is the list we create for storing the questions and answers from the Trivia.txt file, line by line.
  6. TRIVIA[0]: This is how we get the first value from the TRIVIA list. 0 is an index number, and the index of every list starts from number 0.
  7. wrong_choice_1: This is the variable for the first wrong answer choice of the current question.
  8. wrong_choice_2: This is the variable for the second wrong answer choice of the current question.
  9. question = TRIVIA[0]: This is an assignment that copies the first value from the TRIVIA list to the variable for the current question.
  10. answer = TRIVIA[1]: This is an assignment that copies the second value from the TRIVIA list to the variable for the correct answer.
  11. wrong_choice_1 = TRIVIA[2]: The variable for the first wrong answer is copied from the third value of the TRIVIA list.
  12. wrong_choice_2 = TRIVIA[3]: The variable for the second wrong answer is copied from the fourth value of the TRIVIA list.
  13. if-else: In if-else statements of Python conditions, the if statement contains the block of statements that will be executed under the if expression, when the expression is true. The else statement contains the other block of statements that will be executed under the else expression, when the expression is not true. The syntax is like this:
if expression:
   statements
else:
   statements
  1. mouse.get_pos(): This is a pre-defined function in the mouse module of the pygame package, which we use to get the mouse cursor position.
  2. collidepoint(): This is another pre-defined function in the pygame package, which we use to test if a point is inside a rectangle.
  3. if answer_1_rect.collidepoint(mouse_position): This is the if expression that checks whether the mouse position is inside the rectangle of the text of our first answer choice (when the player clicks the answer).
  4. Function and its parameter(s): A function can be computing or executing one or more jobs. The parameter(s) can be a variable, number, or string that is passed into the function.
  5. display_codala(correct_a, "correct_text"): display_codala() is a function on Line 36, which displays an image of Mrs. Codala and the text letting the player know whether the answer is correct or not, which is determined by the parameters when we call the function. In this line of code, correct_a is the variable for the image CorrectAnswerA, and "correct_text" is the string for the if condition that will display the text, “That is correct”. Here, we call the function with these parameters to let the player know that the answer choice is correct. (We already wrote this function in the init.py file.)
  6. else: The else: statement on Line 38 provides an alternative if the if statement (on Line 35) isn't true. In your ClassroomQuiz.py template file, the line is commented out, and you'll need to delete the # symbol to run the line. For an else statement to run, it must be indented at the same level as the if statement above it. In this case, the if statement is on Line 35. If the else statement is not indented at that level, then your code will not run properly. The code won't throw any errors about the improper indentation, so if your program isn't running like it should, you should check to make sure the else statement is indented at the correct level.

3. Advice on Writing the Code

This section provides some hints on how to write your code!

The if statement on Line 31 checks if the player clicks anywhere on the screen. Line 33 checks if the player clicks one of the three potential answers. Line 34 checks if the player clicks the first answer on the top. Line 35 checks if the first answer is the correct answer. If it is correct, Line 36 displays Mrs. Codala and the text that affirms the answer is correct.

And now you're ready to write some code! But we're going to start with an easy step: You're going to uncomment two lines of code.

After Line 37, in your template file, uncomment Lines 38-39, to make the else statement run when the answer is incorrect. Line 39 displays Mrs. Codala's reaction and the text for an incorrect answer.

To uncomment code, you delete/remove the hashtag symbols at the front (or left side) of the code on the line.

Here are what Lines 38-39 look like without the hashtags at the front of the lines:

else: 
    display_codala(incorrect_a, "incorrect_text") 

Next you'll see this comment on Line 41: #TODO: Copy the code here for the second answer. To keep it simple, you're just going to copy the code on the next few lines, under this comment!

Copy in this code for Line 42, which checks if the player clicks the second answer:

if answer_2_rect.collidepoint(mouse_position):

Make sure your code is indented to the right of Line 33, so your indentation level should be at the same level as Line 34.

Copy in this code for Line 43, which checks if answer 2 is the correct answer:

if ANSWER_CHOICES[1] == answer: 

Note that answer 2 is stored in the #1 index position in the ANSWER_CHOICES[] list, because the first answer is stored in the #0 position in the list (which is also called an array). Make sure your code is indented to the right of Line 42, which means it should be at the same level as Line 35.

Copy in this code for Line 44, which displays Mrs. Codala and the corresponding text if the answer is correct:

display_codala(correct_b, "correct_text")

Make sure your code is indented to the right of Line 43, which means it should be at the same level as Line 39.

Copy in this code for Line 45, which runs if the second answer is incorrect:

else:

Make sure your code is indented at the same level as Line 43.

Copy in this code for Line 46, which displays Mrs. Codala and the corresponding text, if the answer is incorrect:

display_codala(incorrect_b, "incorrect_text")

Make sure your code is indented at the same level as Line 44. Note how Line 46 is similar to Line 44. The only differences are the arguments in the display_codala() function, so that this time you display the image of Mrs. Codala and the text that tells the player that they got the answer wrong.

Next, you'll see the comment on Line 48: #TODO: Write the code here for the third answer. You're going to write out the code that checks if the third answer is correct or not, and then you'll display the corresponding image and text for Mrs. Codala.

On Line 49, make sure you're indented at the same level as Line 42. Type out each part of the code:

  1. Type if at the beginning of the line.
  2. Add a space, and then type in the answer_3_rect.collidepoint() function. This Pygame function retrieves the position on the screen where the player clicks the mouse.
  3. Type mouse_position into the parentheses. Don't include any spaces before or after the variable. This mouse_position variable contains the information about where the player clicked.
  4. Add a colon (:) at the end of the line. This symbol tells Python that we have an indented block of code that corresponds to the if statement.

Here's how Line 49 should look:

if answer_3_rect.collidepoint(mouse_position):

Make sure your code is indented at the same level as Line 42.

On Line 50, make sure you indent to the right of Line 49, so you should now be indented at the same level as Lines 43 and 45. Type out each part of the code:

  1. Type if at the beginning of the line.
  2. Add a space and then type in the ANSWER_CHOICES[] list. This list or array contains the information for the three answers of the current question. The 0 index position in the list is for answer #1. The 1 position in the list is for answer #2. And the 2 position in the list is for answer #3.
  3. Type 2 into the brackets of the ANSWER_CHOICES[] list. Don't include any spaces before or after the number. The number 2 tells Python that we're checking the third answer in the list.
  4. Add a space, and then type in the "is equal to" comparison operator (==). We call it a comparison operator because the operator symbol checks whether the two values are the same (the value in the list and the variable after it).
  5. Add a space, and then type in the answer variable. This variable contains the correct answer. So, if the correct answer is the same as the third answer choice (which the player clicked, based on Line 49), then Python will run the code on Line 51.
  6. Add a colon (:) at the end of the line. This symbol tells Python that we have an indented block of code that corresponds to the if statement. In this case, it's just Line 51.

Once you're done, Line 50 should look almost the same as Line 43. Instead of the number 1 in the ANSWER_CHOICES[] list, Line 50 should show the number 2.

Next, is Line 51. This is the exact same as Line 36! It's also indented at the same level as Lines 46, 44, 39, and 36. If you're wondering why Line 36 (for answer #1 being correct) uses the correct_a variable, Line 44 (for answer #2 being correct) uses the correct_b variable, and then Line 51 uses the correct_a variable again, it's because the variables are alternating between two different images of Mrs. Codala that we display to the user.

Line 52 is the else statement again. You should be indented at the same level as Lines 50, 45, and 43. Don't forget to include the colon at the end of the line! This line should look the exact same as Line 45.

The last line that you're going to write is Line 53. This is the exact same as Line 39! It's also indented at the same level as Lines 51, 46, 44, and 39. Note that you don't have to include the comment after the code, which we wrote on Line 39.

That's it! Run the code, because you just completed the game!

Is it still not working? Then you can get more help...

4. The Final Code

I included a solution file for Level 3: Classroom Quiz. This file has all the code filled in, so if you run into any issues with your code (for example, if it doesn't compile/run, or if something isn't working correctly in your program), then you can take a look at the final code file to see what you did differently:

IMPORTANT: Please don't cheat yourself! Finish the game first!

Next Steps

Next, you can take on the two extra challenges 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.

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** ⚠️