Level 3: Challenge 2 - IncrediCoders/Python1 GitHub Wiki

GitHubAvatar copy2

Annie Conda added this page on March 15, 2023


Let's begin your second challenge for Level 3!

In this challenge, you are going to add a third fake answer to every question which will total 4 choices!

To find the Level 3 Challenge 2 code template, open the Level 3 folder, the Challenges folder, and then the Challenge 2 folder. You should already have the files downloaded onto your computer (see Load the IncrediCoders Files). Open the ClassroomQuizChallenge2.py file in Visual Studio Code to build the program by following along with my instructions below!

Explaining the Code

On Line 1, from init import *, initializes the program (init). This includes code that sets up the file path for the images and text file, gets the fonts ready for this game, sets the window size, and loads the images. This statement allows you to use all the information in that file, for the rest of your program.

On Line 3, you'll see the comment, #We pull each line of text from the file into a list. As usual, comments don't do anything when you run the code (they just tell you information when you're looking at your files). This comment tells you what you will be doing for the next line (for Line 4). In this case, you will put the text from the file into a list.

On Line 4, the code TRIVIA = read_file('Assets/Trivia.txt') takes the trivia text from the file and puts it in the "TRIVIA" list variable.

On Line 6, you'll see the comment, #We now have a list of variables that holds our current question, correct answer, and the two wrong choices, in that order. This comment continues onto another comment on Line 7.

On Line 7, we have the comment, #We use these variables to display text on the screen. and including the information from Line 6, saying that you will create a list variable that has the text from the file for the current question, the correct answer (Line 9), and the two wrong choices (Lines 10-11). It has to be in that order.

On Line 8, question = TRIVIA[0], you are creating a variable for the question which was the first task from Line 6, and setting it to the first place in the "TRIVIA" list you created on Line 4.

On Line 9, answer = TRIVIA[1], you are creating a variable for the answer which was the second task from Line 6, and setting it to the second place in the "TRIVIA" list you created on Line 4.

On Line 10, wrong_choice_1 = TRIVIA[2], you are creating a variable for the first wrong choice which was the third task from Line 7, and setting it to the third place in the "TRIVIA" list you created on Line 4.

On Line 11, wrong_choice_2 = TRIVIA[3], you are creating a variable for the second wrong choice which was the fourth task from Line 7 and setting it to the fourth place in the "TRIVIA" list you created on Line 4.

On Line 13, ANSWER_CHOICES = [answer, wrong_choice_1, wrong_choice_2], you are creating a new variable called "ANSWER_CHOICES" where the first place in this list is the correct answer, the second place is the first wrong choice, and the third place is the second wrong choice, all from the variables you created on Lines 9-11.

On Line 14, randomize_answers(ANSWER_CHOICES) #Shuffles the first set of answer choices, this randomizes where the answer, and the wrong choices are when displayed making it unknown to the user where the correct answer is.

On Line 16, line_number = 0, this creates a new variable "line_number" and sets it to ), which is the starting place for later when you go through each of the different lines.

On Line 17, number_of_questions = 12, this creates a new variable "number_of_questions" which is the amount of questions that there are going to be, in this case, the number of questions is set to 12.

On Line 19, display_intro_screen() #Displays the intro screen, the first part does what the second part (a comment) describes which is to display the intro screen which is the screen before the questions.

On Line 21, running = True, this creates a new variable, "running", setting it to "True" which means that the program is running.

On Line 23, #This displays the question screen until the last question or until the player closes the window, this is a commented line that describes what will be run in this program. In this case it is the question screen that will be shown, and then it is closed when that last question is shown or when the player closes the window.

On Line 24, while running:, is the start of a while loop and since in Line 21, you set "running" to True, this line can also be read as "while True" and then the following lines of code will run when this is the case.

On Line 25, display_question(question, ANSWER_CHOICES) #Displays the new question and the three answer choices, as the comment describes, displays the new question and the three answer choices which is the starting screen for this game.

On Line 26, EVENTS = pygame.event.get(), sets the variable "EVENTS" to the list that came from pygame.

On Line 27, for event in EVENTS:, is a nested "for loop" which means it is a "for" loop within another loop, in this case within a "while" loop. The following code will run until there are no more events in the list "EVENTS".

On Line 28, if event.type == pygame.QUIT: #If the player clicks the Close button, it exits the game, is a nested "if" loop which means it is an "if loop" within another loop which similar to Line 29, it is within the "while" loop. This checks to see if the "type" of command in this list is "pygame.QUIT", if it is, then the program will quit out.

On Line 29, running = False, sets the variable "running" to False, originally it had been set to True.

On Line 30, mouse_position = pygame.mouse.get_pos(), sets the variable "mouse_position" to a function on pygame which tracks where your mouse is on the screen.

On Line 31, if event.type == pygame.MOUSEBUTTONDOWN: #If the player clicks the mouse, is another nested if loop which I described on Line 30, and this will be true whenever you click your mouse button down which be helpful for determining which answer is selected.

On Line 32, if answer_1_rect.collidepoint(mouse_position): #If the player clicks the 1st answer on the top, is another nested if loop which checks to see if the answer selected was the first answer on the top, if so it will run the code below it that is indented.

On Line 33, if ANSWER_CHOICES[0] == answer: #If it's the correct answer, is telling you that if the answer that you chose is correct, it will run the code below it that is indented.

On Line 34, #TODO: Uncomment these lines to add interactive effect, is a comment that instructs you to uncomment the following lines which I will describe what they do on each of the individual lines.

On Line 35, ##Change color from blue to green, you want to change the current color (blue) to green.

On Line 36, #answer_1_text = my_font.render(answer, True, (0,128,0)), you want to uncomment this line which will display and load the text for the first question.

On Line 37, #screen.blit(answer_1_text, answer_1_rect), you want to uncomment this line as well which will draw out the answer to the first question that was displayed on Line 36.

On Line 38, #pygame.display.update(), you will want to uncomment this line as well, and it will update the display to show the question and the answer.

On Line 39, #pygame.event.get(), after you uncomment it, will get the next event in the "pygame.event" list.

On Line 40, #time.sleep(1), after this line is uncommented, it will start the timer to close the window if the player does not continue on interacting with the game.

On Line 41, display_codala(correct_a, "correct_text") #Displays codala and text for correct answer, since this was the correct answer choice, Codala will be displayed with her text bubble saying that the player got the correct answer.

On Line 42, else: #If it's an incorrect answer, since this was the incorrect answer choice, Codala will be displayed with her text bubble saying that the player got the incorrect answer which is something that you will write in yourself.

Write Your Own Code

On Line 43, #TODO: Add interactive effect, in the next lines, you will be adding the interactive effect described on Line 44.

On Line 44, #Change color from blue to red, you want to write out in the next lines the code that will change the color from blue to red.

On Line 45, look back at Line 36 to write out the first step for changing the color from blue to red.

On Line 46, look back at the second step to changing the color on Line 37 that makes sense to go after what you wrote on Line 45.

On Line 47, next you want to write the code to update the color from pygame looking back at Line 38.

On Line 48, the next step you want to do is get the information from pygame looking back at Line 39.

On Line 49, the final step is setting the timer for if the player is inactive, to do this look back at Line 40.

On Line 50, display_codala(incorrect_a, "incorrect_text") #Display codala and text for incorrect answer, since this was the incorrect answer choice, Codala will be displayed with her text bubble saying that the player got the incorrect answer.

On Line 51, if answer_2_rect.collidepoint(mouse_position): #If the player clicks the 2nd answer, similar to Line 32, the program will read if the player selected the second answer from the top.

On Line 52, if ANSWER_CHOICES[1] == answer:, if the answer that the player selected was correct, the following indented code will be run.

On Line 53, #TODO: Add interactive effect, is what you are going to do on the next lines which I describe on Line 54.

On Line 54, #Change color from blue to green, is what you will do on the following lines, changing the color from blue to green.

On Line 55, you first want to display the text like you did on previous lines look back to what you did on Line 45 for guidance.

On Line 56, next you want to draw on the screen the text like you did on previous lines look back to what you did on Line 46 for guidance.

On Line 57, next you want to update the pygame program like you did on Line 47.

On Line 58, the next step that you want to write out is getting the information from the pygame file like on Line 48.

On Line 59, the final step is you want to write out the code for a timer in case the player does not continue with the game like on Line 49.

On Line 60, display_codala(correct_b, "correct_text"), this will display Codala's speech bubble saying that the answer was correct!

On Line 61, else:, if the answer is incorrect, the following lines that are indented will be run.

On Line 62, #TODO: Add interactive effect, this is a comment that describes what you will do on the next lines which I described on Line 63.

On Line 63, #Change color from blue to red, is the goal you want to achieve on the next lines.

On Line 64, you first want to display the text like you did on previous lines look back to what you did on Line 45 for guidance.

On Line 65, next you want to draw on the screen the text like you did on previous lines look back to what you did on Line 46 for guidance.

On Line 66, next you want to update the pygame program like you did on Line 47.

On Line 67, the next step that you want to write out is getting the information from the pygame file like on Line 48.

On Line 68, the final step is you want to write out the code for a timer in case the player does not continue with the game like on Line 49.

On Line 69, display_codala(incorrect_b, "incorrect_text"), this will display Codala's speech bubble saying that the answer was incorrect. On Line 70, if answer_3_rect.collidepoint(mouse_position): #If the player clicks the 3rd answer

On Line 71, if ANSWER_CHOICES[2] == answer: this is a true or false statement checking to see if the answer choice is the correct answer (true) or not (false).

On Line 72, #TODO: Add interactive effect , this describes what you will be writing on the next lines which I describe more on Line 73.

On Line 73, #Change color from blue to green, is the goal for the next lines, to change the color from blue to green.

On Line 74, you first want to display the text like you did on previous lines look back to what you did on Line 45 for guidance.

On Line 75, next you want to draw on the screen the text like you did on previous lines look back to what you did on Line 46 for guidance.

On Line 76, next you want to update the pygame program like you did on Line 47.

On Line 77, the next step that you want to write out is getting the information from the pygame file like on Line 48.

On Line 78, the final step is you want to write out the code for a timer in case the player does not continue with the game like on Line 49.

On Line 79, display_codala(correct_a, "correct_text"), since this was the correct answer, Codala will be displayed with her text bubble saying that the player got the correct answer.

On Line 80, else:, the following lines will run if it is the incorrect answer which you will write yourself.

On Line 81, #TODO: Add interactive effect, as you did before, you will be writing code for an interactive effect described on Line 81.

On Line 82, #Change color from blue to red, your goal for the next lines is to change the color from blue to red.

answer_3_text = my_font.render(ANSWER_CHOICES[2], True, (255,0,0)) screen.blit(answer_3_text, answer_3_rect) pygame.display.update() pygame.event.get() time.sleep(1) display_codala(incorrect_a, "incorrect_text") On Line 83, you first want to display the text like you did on previous lines look back to what you did on Line 45 for guidance.

On Line 84, next you want to draw on the screen the text like you did on previous lines look back to what you did on Line 46 for guidance.

On Line 85, next you want to update the pygame program like you did on Line 47.

On Line 86, the next step that you want to write out is getting the information from the pygame file like on Line 48.

On Line 87, the final step is you want to write out the code for a timer in case the player does not continue with the game like on Line 49.

Copy the Code

On Line 88, display_codala(incorrect_a, "incorrect_text"), since this was the incorrect answer, Codala will be displayed with her text bubble saying that the player got the incorrect answer.

On Line 90, #Checks if it's the last question and display end screen, This is describing what the next line of code which I will describe on that line.

On Line 91, running = check_if_last_question(line_number, running, number_of_questions), this is a true or false question, if false, the program will quit, and if true it will continue on to the next question.

On Line 92, #If it's the not the last question, we display the next question, is a commented line that describes if there are more than twelve questions, then line 74 will be displayed.

On Line 93, line_number, question, answer, ANSWER_CHOICES = move_to_next_question(TRIVIA, question, line_number, ANSWER_CHOICES), displays the text for if there is more than twelve lines of code.

On Line 94, pygame.quit(), this line of code quits of the pygame program.

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: Help - This page helps you complete the instructions in the book, in case you get stuck.

  • Level 3: Challenge 1 - On this page, I show you how to add a score to the game, so you can get all the questions right, then when the game ends, displaying the score.

  • Level 3: Online Articles - I made you a list of different web pages I found, which will help you learn more about the 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 pick a character and an animal with your friend, then you flip a coin and if it lands on heads, you will act like your character and if it lands on tails, you will act like your animal!

  • Level 3: Rewards - If you completed the Classroom Quiz 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 Bow 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 for how to build the Space Wars program:

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