Cloud Object Storage | Store & Retrieve Data Anywhere ...



Hangman gameEstimated duration: 2 lessons of 45 minutes eachAge level: Lower secondary school studentsLearning objectives, skills and competencies:Develop and test a program using Python programming language and Trinket.ioUse conditionals (if-else) to make decisions in your programUse loops to iterate over a sequence Use loops to execute a block of instructions as long as condition is trueUse indexing to access any item in a list or any character in a stringImplement fundamental data structures and associated algorithmsActivities and roles:Students: develop a simplified version of a Hangman game (the user suggests the letters to guess a hidden word within a certain number of guesses) using Python programming language.Teacher: monitors and demonstrates the activity, assists when needed.What you will need:One computer per student connected to the internet or offline computers with Python’s Integrated Development and Learning Environment (IDLE) installed. If you don’t have enough computers, encourage your students to work in pairs or small groups (2-4 students).Learning space: Computer classroomActivity description:In this activity students will create a simplified version of a Hangman game. The computer will randomly choose a word from a list, and the user has 6 guesses to guess the word.*Tip: To help you with your first program in Python, we have left you some guidance in comments in the code below. These are marked with green and always start with a #. You can also leave comments in your code like this to help your students.Preparation:You can start this activity by playing a game of hangman at or offline (one student can think of a word and others can try to guess it).Go to the website, and you are ready to create your first project. You can write your first line of code by typing in the left window. After clicking the run button, you’ll see the output (how your code would look for the users) in the right window. Step 1: Adding list of wordsTo generate random words, we need to use a module: random. Modules have to be imported into our program by putting one line of code at the beginning:We want to count our wrong answers and end the game if the user has more than 6 wrong answers. We’ll add a variable to count our wrong answers and set its value to 0 (because we have made no mistakes yet). Variables are used to store values and give data to the computer for processing. They can store data of different types that can do different things. Our variable here is wrong and it’s a number.Now we can define our word list by assigning a list to the words variable, and let the computer choose one random word using the random module:Step 2: The first outputsOur next variable will be the guess of the user, we’ll call it guess. We will only show dashes (“_ _ _ _ _”) in the beginning, as many dashes as the length of the word is.We can now print a welcome message on the screen:Step 3: Loops and game logic For setting up the logic of the game, we’ll use the while loop. If your students are not familiar with the while loop, you can show them this short animation.We’ll enter a while loop and ask the user to enter a letter as long as two conditions apply:Number of wrong answers is less than 6 (wrong<6)Their guess is different from the hidden word (guess!=hidden_word)All the indented lines after the colon belong to the same block of code. They will repeat as long as our conditions are met:Next step is to check if the user input (letter) exists in the randomly chosen word. 160020589280If the letter exists in the hidden word, first, we have to change our guess and add this new letter. guess is a string type of variable (sequence of characters), which is immutable (cannot be changed), therefore, we need to create a new string every time we want to change it. We can do this by assigning an empty string to our new guess variable. Strings in Python are surrounded by quotation marks. We are creating an empty string, so we’ll just put quotation marks:To check if the letter entered by the user is in the hidden word, we’ll use the for loop to go through the hidden word and compare every letter of the hidden word with the letter entered by the user.35623510610850Variable i is the counter/iterating variable, which starts with 0 and increments in each iteration step of the loop. Our loop will repeat len(hidden_word) number of times, which is the length of the hidden word.If the user input is found in the hidden word, we’ll track its position in the hidden word by the help of an index. We’ll change our guess to our new guess variable and add the guessed letter. Otherwise we’ll just put _ to the same place.510540127889000If your students are not familiar with indexing strings, now is a good time to introduce it. Each character of a string corresponds to an index number, starting with 0. We can access any index number and associated character by putting the index number in square brackets:Output result:The last thing we need to do is to swap the variable value of the new guess (new) and the old guess (guess):411480195199000Step 4: Ending the game Now let’s see what happens if the user input is incorrect:increase value of the wrong variable by oneprint incorrect message (“INCORRECT!”)95250199072500-635000After checking the user input, we will print the guess word and hidden_word:left243141500Ask your students how this game can be improved. Let them think about how it can be more interesting or challenging to pleted project: template: of author: Ivana Vezjak ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches