Level 2: Challenge 2 - IncrediCoders/Python1 GitHub Wiki

Scratcher_PenThink

Mrs. Scratcher added this page on March 14, 2023


Let's begin your second challenge for Level 2!

In this challenge, instead of using an if/else ladder, you'll put the text and images into a Python list!

To find the Level 2 Challenge 2 code template, open the Level 2 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 ClassIntroductionsChallenge2.py file in Visual Studio Code to build the program by following along with my instructions below!

Explaining the Code

On Line 1, you'll see from init import * #Gives us many helpful functions. Like in Level 1, this line of code initializes each of the functions that will be used throughout this project.

On Line 3, #Loads the background and images, this is a comment as it has a "#" before the text, which means it is describing what will happen in the next lines: Lines 4 loads the background image, and Lines 5-18 load the classmates' images.

On Line 4, background = load_file("Assets/Background.png"), loads the background picture into the background variable. This way you can see the image of my classroom on your window as the background.

On Line 5, annie_conda = load_file("Assets/AnnieConda.png"), loads the picture of the character Annie Conda into the annie_conda variable. This variable will be used later to display the Annie Conda image on the window.

On Line 6, bayo_wolf = load_file("Assets/BayoWolf.png"), loads the picture of the character Bayo Wolf into the bayo_wolf variable. This variable will be used later to display the Bayo Wolf image on the window.

On Line 7, grafika_turtle = load_file("Assets/GrafikaTurtle.png"), loads the picture of the character Grafika Turtle into the grafika_turtle variable. This variable will be used later to display the Grafika Turtle image on the window.

On Line 8, intelli_scents = load_file("Assets/IntelliScents.png"), loads the picture of the character Intelli Scents into the intelli_scents variable. This variable will be used later to display the Intelli Scents image on the window.

On Line 9, java_lynn = load_file("Assets/JavaLynn.png"), loads the picture of the character Java Lynn into the java_lynn variable. This variable will be used later to display the Java Lynn image on the window.

On Line 10, captain_javo = load_file("Assets/CaptainJavo.png"), loads the picture of the character Captain Javo into the captain_javo variable. This variable will be used later to display the Captain Javo image on the window.

On Line 11, jitter_bug = load_file("Assets/JitterBug.png"), loads the picture of the character Jitter Bug into the jitter_bug variable. This variable will be used later to display the Jitter Bug image on the window.

On Line 12, paul_python = load_file("Assets/PaulPython.png"), loads the picture of the character Paul Python into the paul_python variable. This variable will be used later to display the Paul Python image on the window.

On Line 13, quackintosh = load_file("Assets/Quackintosh.png"), loads the picture of the character Quackintosh into the quackintosh variable. This variable will be used later to display the Quackintosh image on the window.

On Line 14, sb_turtle = load_file("Assets/SBTurtle.png"), loads the picture of the character SB Turtle into the sb_turtle variable. This variable will be used later to display the SB Turtle image on the window.

On Line 15, sidewinder = load_file("Assets/SideWinder.png"), loads the picture of the character Side Winder into the sidewinder variable. This variable will be used later to display the Side Winder image on the window.

On Line 16, syntax_turtle = load_file("Assets/SyntaxTurtle.png"), loads the picture of the character Syntax Turtle into the syntax_turtle variable. This variable will be used later to display the Syntax Turtle image on the window.

On Line 17, ram_rom = load_file("Assets/RAMROM.png"), loads the picture of the character RAM & ROM into the ram_rom variable. This variable will be used later to display the RAM & ROM image on the window.

On Line 18, amphib_ian = load_file("Assets/AmphibIan.png"), loads the picture of the character Amphib Ian into the amphib_ian variable. This variable will be used later to display the Amphib Ian image on the window.

On Line 20, #Stores the character text into the variables you can see there is a "#" before the text, and as you remember this is a commented line. This comment describes what the next lines will do. As the comment tells, next lines (Line 21 to 34) will store the character texts into the variables.

On Line 21, text_annie_conda = "Hello! I'm Annie Conda. \nI come from Sanfran-Hissco, Cowlifornia. I've done a little coding. My favorite musician is Justin Timbersnake. I'm also partial to Hissy Elliott. My favorite Pigxar movie is Rattle-toulle. I love to make trivia games and word games.", stores the character text of Annie Conda into the text_annie_conda variable to use with Annie Conda's character image.

On Line 22, text_bayo_wolf = "I'm Bayo Wolf, from Little Squawk, Barkansas. I'm the best at SpaceWars and great at Mega Mechs in my Grendel mech. My favorite movies are The Dogfather, Jurassic Bark, Citizen Canine, and Stall Wars: The Empire Strikes Cats. My top actors are Brad Pitbull, Howly Berry, and Sandra Bulldog.", stores the character text of Bayo Wolf into the text_bayo_wolf variable to use with Bayo Wolf's character image.

On Line 23, text_grafika_turtle = "My name is Grafika Turtle. I live here, in Red-mutt, Washeepton. Now I get to go to school with my best friend. Hi, Paul! I love the movie Wizard of Paws, and my favorite artist is Pablo Pigcasso. I like coding in Turtle Graphics, and my brother Syntax and I are pretty good at coding card games.", stores the character text of Grafika Turtle into the text_grafika_turtle variable to use with Grafika Turtle's character image.

On Line 24, text_intelli_scents = "Hi. I'm Intelli-Scents from Minnea-pawlis, Minnow-soda. My top movies are Hack to the Future, Mission Impawsible with Eat'n Hunt, and Hair-Spay. My top artist is Vincent van Gopher, top book is The Time Machine by H.G. Gills, and my favorite neurologist is Digmund Freud.", stores the character text of Intelli Scents into the text_intelli_scents variable to use with Intelli Scents' character image.

On Line 25, text_java_lynn = "I'm Java Lynn, also from Minnea-pawlis. I shop at Blooming Tails and read Vanity Fur. Top movies are Hairy Otter 8, Catsaway with Tomcat Hanks, and the Sound of Mew-sic. My actors are Bill Furry and Scarlett Johamster. I love the art \"Squirrel with the Acorn Earrings.\" ", stores the character text of Java Lynn into the text_java_lynn variable to use with Java Lynn's character image.

On Line 26, text_captain_javo = "I'm Captain Javo, from Indiana-pawlis, Fin-diana. The movies I like are The Fast and Furry-us, Paws, and Clawshank Redemption. My actors are Woodchuck Norris, Billy Grrr-ystal, and Will Ferret. My musicians are Kitty Purry and Britney Ears. My favorite programming language is Java. ", stores the character text of Captain Javo into the text_captain_javo variable to use with Captain Javo's character image.

On Line 27, text_jitter_bug = "I am Jitter Bug, from Ant-aheim, Cowlifornia. My favorite movies are Mrs. Doubtspider and Twi-mite. I love the Stall Wars character Luke Flywalker. My top actors are Kristin Ear-Wiig and Molly Ringworm. My favorite musicians are Beeyonce, Flyley Flyrus, and Nine-Inch Snails. ", stores the character text of Jitter Bug into the text_jitter_bug variable to use with Jitter Bug's character image.

On Line 28, text_paul_python = "Hi. I'm Paul Python. My home is just over the bridge in Sea-cattle. Mega Mechs is my favorite game, and the reason why I'm here, by winning the tournament! I love the actor David Hisselhoff, and I agree with Annie that Justin Timbersnake makes awesome music! But I also like White Snake. ", stores the character text of Paul Python into the text_paul_python variable to use with Paul Python's character image.

On Line 29, text_quackintosh = "Hello. I'm Quackintosh, from nearby Bill-view, Washeepton. My top actors are Audrey Honkburn, Goose Willis, Squawkin Phoenix, Robird De Niro, Hennifer Lawrence, and Woody Owlen. My musicians are Swan Bon Jovi, Michael Quackson, and Ozzy Ostrich. My top art is \"Son of Duck.\" ", stores the character text of Quackintosh into the text_quackintosh variable to use with Quackintosh's character image.

On Line 30, text_sb_turtle = "Hey. I'm SB Turtle, from New-ark, Moo Jersey. My top actresses are Shelly Long and Zooey Deshell. I'm a founding member of the Shell Scouts, and my favorite programming language is Microsoft Small Basic. Oh, and I grow into a giant monster, but that's for a different book. " stores the character text of SB Turtle into the text_sb_turtle variable to use with SB Turtle's character image.

On Line 31, text_sidewinder = "I'm SideWinder, from Salt-Snake City, Mewtah. I'm the best at SpaceWars, despite what Bayo thinks. My favorite movie is Snakes on a Glider, my favorite actor is Sylvester Stallion, and my favorite musician is Nine-Inch Snails. Jitter Bug has good taste. ", stores the character text of Side Winder into the text_sidewinder variable to use with Side Winder's character image.

On Line 32, text_syntax_turtle = "What's up? Syntax Turtle in the house. I'm from here, Red-Mutt, with my twin sis Grafika. My favorite actress is Natalie Porkman, and my favorite artist is Lizardnardo Da Vinci. My top games are \"Skate and Fly\" and Porkymon, and I'm looking forward to making a Codu Kids card game! ", stores the character text of Syntax Turtle into the ``text_syntax_turtle variable to use with Syntax Turtle's character image.

On Line 33, text_ram_rom = "Heya! We're RAM and ROM. We came from the other side of the country, Woolshington DC. We like Meryl Sheep, Dustin Hoofman, and Eva Longhornia. Our favorite musician is Lady Baa-Baa, and our sensei is the Dali Llama. We're working on a top-secret project! ", stores the character text of RAM and ROM into the text_ram_rom variable to use with their character images.

On Line 34, text_amphib_ian = "Yo, yo. What's hopping, peeps? Name's Amphib Ian. My launchpad is Croaklahoma City. My choice singer is Demi Lovatoad. And my fave artwork is \"Mourning Son\" by Edwart Hopper. My go-to game to code is Froggy Road. That's it. I'll catch you on the flip flop! ", stores the character text of Amphib Ian into the text_amphib_ian variable to use with Amphib Ian's character image.

Uncomment the Code

You're just going to uncomment (remove the hashtag) for Lines 40, 47, and 48.

On Line 37, the comment #Initializes the lists explains that Lines 38 and 40 initialize the two list variables to contain the characters and their introduction text.

On Line 38, CHARACTERS = [] initializes the list variable. A list variable is a kind of variable that stores a list of items. Currently, the list is empty, but we'll add the characters' variables into this list.

On Line 39, #TODO: Uncomment the line below to initialize the TEXT list instructs you to uncomment the next line, Line 40, so that it will be included when you run the code.

On Line 40, uncomment the #TEXT = [] code. This line initializes the TEXT list, for storing all the introduction text that was said by the characters. This list is also empty at this point.

On Line 42, the comment #Adds the characters and text to the lists explains that Lines 43-44 add the characters and their text messages to the two lists.

On Line 43, the code CHARACTERS.append(annie_conda) adds the annie_conda variable, which adds Annie Conda's image variable in the end of the CHARACTERS list. The append() function is used in Python for adding new items into the end of a list.

On Line 44, TEXT.append(text_annie_conda) adds the text from Annie Conda's introduction, which is stored in the text_annie_conda variable, to the end of the TEXT list.

On Line 46, the comment #TODO: Uncomment the two lines below instructs you to remove the hash tags on Lines 47-48, so that they are included when you run your code.

On Line 47, uncomment #CHARACTERS.append(bayo_wolf) by removing the hash tag at the beginning of the line. After you uncomment this line, this code adds the character Bayo Wolf's image variable to the end of the CHARACTERS list.

On Line 48, uncomment #TEXT.append(text_bayo_wolf) to add Bayo Wolf's introduction text to the end of the TEXT list.

Copy the Code: Add Grafika and Intelli-Scents

On Line 50, the comment #TODO: Add the rest of the characters to the lists instructs you to add the rest of characters to the CHARACTERS list and the introduction text messages to the TEXT list, just like you did in Lines 43, 44, 47, and 48.

On Line 51, you'll need to add Grafika Turtle to the character list. Type in the following code: CHARACTERS.append(grafika_turtle)

On Line 52, you'll add Grafika Turtle's class introduction to the TEXT list. You'll use the TEXT.append() function from Line 48. The difference is that you'll include the text_grafika_turtle variable to make sure it's Grafika's text that gets displayed on the background. Type in the code: TEXT.append(text_grafika_turtle)

On Line 53, you're going to add IntelliScents to the character list. This is similar to Lines 47 and 51. You'll use the CHARACTERS.append() function, and you'll send the intelli_scents image variable to the function. This will be used to show the IntelliScents background image on the screen. Type in the code: CHARACTERS.append(intelli_scents)

On Line 54, you'll add IntelliScents' class introduction to the text list. This is similar to Line 52, where you set up Grafika's text to display it on the screen. Type in the code: TEXT.append(text_intelli_scents)

Write Your Own Code: Adding the Other Characters

On Line 55, you're going to add Java Lynn to the character list. This is like the code you wrote on Line 53, for IntelliScents' background image. Type in the CHARACTERS.append() function. Inside the parentheses, type in the java_lynn variable.

On Line 56, you'll add Java Lynn's class introduction to the text list. This is similar to the code you wrote on Line 54. Type in the TEXT.append() function. Inside the parentheses, type in the text_java_lynn variable. This sets up Java Lynn's introduction text to show on her background image.

On Line 57, you want to add Captain Javo to the character list. This is like the code you wrote on Lines 53 and 55. Type in the CHARACTERS.append() function. Inside the parentheses, type in the captain_javo variable.

On Line 58, you want to add Captain Javo's class introduction to the text list. This is similar to the code you wrote on Lines 54 and 56. Type in the TEXT.append() function. Inside the parentheses, type in the text_captain_javo variable.

On Line 59, you're going to add Jitter Bug to the character list. Use the same function that you did on Line 57. This time, type in the jitter_bug variable, which acts as the function's argument. This sets up the Jitter Bug background image to be displayed.

On Line 60, you're going to add Jitter Bug's class introduction to the text list. Use the same function that you did on Line 58 for Captain Javo. This time, type in the text_jitter_bug variable, which acts as the function's argument. This sets up the Jitter Bug introduction text to be displayed over her background image.

On Line 61, you'll add Paul Python to the character list. Use the same function that you did on Line 59. This time, type in the paul_python variable.

On Line 62, you'll add Paul Python's class introduction to the text list. Use the same function that you did on Line 60. This time, type in the text_paul_python variable.

On Line 63, you'll add Quackintosh to the character list. Copy Line 61 and use the quackintosh variable.

On Line 64, you'll add Quackintosh's class introduction to the text list. Copy Line 62 and use the text_quackintosh variable.

On Line 65, you'll add SB Turtle to the character list. Copy Line 63 and use the sb_turtle variable.

On Line 66, you'll add SB Turtle's class introduction to the text list. Copy Line 64 and use the text_sb_turtle variable.

On Line 67, you'll add Sidewinder to the character list. Copy Line 65 and use the sidewinder variable.

On Line 68, you'll add Sidewinder's class introduction to the text list. Copy Line 66 and use the text_sb_turtle variable.

On Line 69, you'll add Syntax Turtle to the character list. Copy Line 67 and use the syntax_turtle variable.

On Line 70, you'll add Syntax Turtle's class introduction to the text list. Copy Line 68 and use the text_sytax_turtle variable.

On Line 71, you'll add RAM and ROM to the character list. Use the function from Line 69 and use the RAM and ROM image variable back from Line 17.

On Line 72, you'll add RAM and ROM's class introduction to the text list. Use the function from Line 70 and use the RAM and ROM text variable back from Line 33.

On Line 73, you'll add Amphib Ian to the character list. Use the function from Line 71 and use the Amphib Ian image variable back from Line 18.

On Line 74, you'll add Amphib Ian's class introduction to the text list. Use the function from Line 72 and use the Amphib Ian text variable back from Line 34.

Explaining the Code: Set the Key Events

On Line 76, running = True creates a new variable, running, and sets its value to True. This sets up the loop on Line 79 to always run, until the running variable is set to False instead (see Line 86).

On Line 77, #Starts at the first character in the list is a comment that states that the while running loop will start with the first character (the first item in the list).

On Line 78, index_number = 0 creates a new variable to keep track of where you're at in the list, and as the comment in Line 77 stated, it starts at the first character, which is the 0th item in the list.

On Line 79, while running: starts the loop, and since you set the variable, running, to True, so it is basically saying "while true." Note that the while loop statement always has a colon at the end of the line.

On Line 80, current_character = CHARACTERS[index_number] sets the current character that you wrote out earlier equal to the current index or place in the character list.

On Line 81, current_text = TEXT[index_number] sets the current text or class introduction that you wrote out earlier equal to the current index or place in the text list.

On Line 82, display(background, current_text, current_character) displays the image of the current character while showing the character's introduction text on the background.

On Line 83, EVENTS = pygame.event.get() calls the get method of the pygame.event library. This sets the EVENTS variable to include all the events from the player. This is in all our programs, because it gathers user input (such as keyboard and mouse activities).

On Line 84, for event in EVENTS: creates a for loop that loops through every event in our EVENTS list, such as a key press or mouse click (on the exit X).

On Line 85, if event.type == pygame.QUIT: will check to see if the player clicks the exit button.

On Line 86, running = False sets the previously created running variable to False. This exits you out of the while loop, and the program ends. It closes the window.

On Line 87, you'll find if event.type == pygame.KEYDOWN: #Checks if the player presses a key. As the comment suggests, this line checks if the player pressed a key. If so, it runs the indented code below the if statement (Lines 90-95).

Copy the Code: Switch Between Pages

Next, you're going to add an event for the Right key, in order to switch between pages.

On Line 89, you'll find the comment #TODO: Write two lines that check if the player presses the RIGHT key, to go to the next character. This is something that you're going to do, but keep the goal in mind of checking if the player presses the Right key (Line 90 checks this) and if so, your program displays the next character in the list (Line 91 does this by changing the value of the index_number variable).

On Line 90, you're going to create the code for the first part of the "#TODO" comment on Line 89: you'll check if the player uses the Right key. Keep the same indentation level as the comment on Line 89.

On Line 90, type the code: if event.key == pygame.K_RIGHT:

On Line 91, you'll complete the second part of the "#TODO" comment that's on Line 89: you'll update the index_number variable to show the next character in the list. Remember to indent (tab) this line, so that it is indented to the right of Line 90. It should be the same indentation level as Line 86.

On Line 91, type the code: index_number = index_number + 1

On Line 93, you should see the comment, #TODO: Write two lines that check if the player presses the LEFT key, to go to the previous character. You're going to check if the player presses the LEFT key (in Line 94) and if so, you're going to display the previous student/character (in Line 95).

On Line 94, you're going to check if the player presses the LEFT key. This line should be at the same indentation level as Line 90.

On Line 94, type the code: if event.key == pygame.K_LEFT:

On Line 95, you're going to display the previous character. Remember to indent (tab) this line, so you're one indentation level to the right of Line 94. You should be at the same indentation level as Line 91. The code is the same as Line 91, but you're going to subtract one number from the index_number variable to move back to the previous character in the list. (On Line 91 you added one to the index_number variable to show the next character in the list.)

On Line 95, type the code: index_number = index_number - 1

Write Your Own Code: Cycle Through Introductions

Next, on Lines 98-101, you're going to cycle through the list so that if you reach end of the list of characters, you start back over from the beginning of the list. And if the users press LEFT at the beginning of the list, then they cycle to the end of the list. For example, when the user gets to the last student in our list, Amphib Ian, and they press Right again, then they start the list over and cycle back to the first person, Annie.

On Line 97, you'll see the comment, #If we reach the end of the list, we start from the beginning again. Line 98 is going to check if the user presses the Right key, after they got to the end of the list of students. If so, Line 99 sets the index_number variable back to the first character in the list. Line 100 checks if the user presses the Left key when the first character is displayed. If so, Line 101 sets the index_number variable to the last character in the list. Let's get to the code!

On Line 98, the line says if index_number == #TODO: Finish this if-statement to check if "index_number" equals the last character in the list. You're going to replace the #TODO comment at the end of the if statement (after the == symbol), with the number 14. This checks if the user was displaying the last character, #13 (Amphib Ian), and then pressed the Right arrow key one more time, to increment the index_number list to equal 14.

On Line 98, after the == symbol, type _14:_ at the end of the line. Be sure to include a colon at the end of the if statement.

On Line 99, you'll see the placeholder comment, #TODO: Add a statement to set "index_number" back to the first character in the list. In Python lists, the first value is stored in the "0" place. You're going to cycle the character list over, so that the user sees the first character again, at the beginning of the list.

On Line 99, set the index_number variable to 0:

  • Type index_number at the beginning of the line.
  • Add a space, and then type the = symbol.
  • Add a space, and then type 0 at the end of the line.

Next, you'll add an if statement that cycles from the first student (Annie) to the last one (Amphib Ian), when you press the Left arrow.

On Line 100, you'll see if index_number == #TODO: Finish this if-statement to check if "index_number" equals the first character in the list. Your goal is to finish this statement. You're going to replace the entire comment, at the end of the line, with -1: instead. We are checking for the value -1, because this shows that the user pressed the Left arrow when displaying the first character, Annie, who is set to the 0 place in the index_number list.

On Line 100, after the == symbol, type _-1:_ at the end of the line. Be sure to include a colon at the end of the if statement.

On Line 101, you'll see the placeholder comment, #TODO: Add a statement to set "index_number" to the last character in the list. We're going to set the index_number variable to 13.

On Line 101, set the index_number variable to 13:

  • Type index_number at the beginning of the line.
  • Add a space, and then type the = symbol.
  • Add a space, and then type 13 at the end of the line.

On Line 103, pygame.quit() ends the program and releases all the resources used to run the program.

Congratulations! You have completed the second challenge of Level 2!

More Level 2 Resources

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

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

  • Level 2: Challenge 1 - On this page, I show you how to add the instruction for RAM and ROM and one for Amphib Ian, totaling 14 class introductions.

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

  • Level 2: 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 2: 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 choose a noun for the variable to say in the story that they tell!

  • Level 2: 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 3

After you're completely done with Level 2 (did you do the challenges?), then it's time to move on to Level 3! While you read through Level 3 in your book, you can check out the resources from Mrs. Codala, as she teaches you how to build the Classroom Quiz program:

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