Python Guessing Game PDF

Summary

This document is a practice assignment for a computer programming class. It guides students through building a number guessing game, using Python. The assignment focuses on random number generation and user input.

Full Transcript

Session 3 Guess a Number then Play Again Let's get started! You will build the first part of the Guess It game. You will follow the plan from Assignment 11 to code each step. The program will: explain how to play pick a random number ask the...

Session 3 Guess a Number then Play Again Let's get started! You will build the first part of the Guess It game. You will follow the plan from Assignment 11 to code each step. The program will: explain how to play pick a random number ask the player to guess the number tell the player if they guessed correctly Look at your plan from Assignment 11. You will turn the words into code. allow the player to play again The program will use the following code to control the game play: CODE PURPOSE Tells the program to get commands from the random import random library. This is needed to pick a random number. play=True Controls the game play. The player can keep playing if the value of the play variable is True. while play: Loops instructions to play the game again. The loop keeps repeating if the value of the play variable is True. number=random.randint(1, 10) Picks a random number between 1 and 10. pick=input('What is your guess? ') Allows the player to guess a number. num_pick=int(pick) Changes the guess from a string to an integer. if number!=num_pick: Checks if the guess is wrong. if number==num_pick: Checks if the guess is correct. again=input('Play again? yes or no ') Allows the player to decide if they want to play again. if again=('yes'): Checks if the person says 'yes' to play again. else: Ends the game if the person does not say 'yes'. play=False The game is over when the value of play is False. Open IDLE and Create a New File 1.  Open IDLE (Python).  From the File menu, select New File.  From the File menu, select Save. Type guess as the file name. Click Save. Copyright © TechnoKids Inc. 53 TechnoPython | Python Session 3 Tell Players About the Game The player needs to know how to play. 2.  Add the game title and instructions. #about the game print('GUESS IT GAME') Double quotes are print("It's time for fun!") used if a word has an apostrophe. print('Guess a number between 1 and 10.') 3.  From the File menu, select Save or press CTRL + S.  From the Run menu, select Run Module.  Read the text: GUESS IT GAME It's time for fun! Guess a number between 1 and 10. >>  Close the Python Shell. Have the Game Pick a Random Number The game must pick a random number. This is done using a command from the random library. 4.  Import the random library and have the game pick a number between 1 and 10: #about the game print('GUESS IT GAME') print("It's time for fun!") print('Guess a number between 1 and 10.') #set up the game import random randint is short for random integer #play game (1, 10) means number=random.randint(1, 10) between 1 and 10. print('The number is', number)  Apply your skills to test the program. Look at the random number: GUESS IT GAME It's time for fun! Guess a number between 1 and 10. The number is 9 >> This line will be used to help you test the program. It tells you the random number that was picked. You will move it later.  Close the Python Shell.  Run the program again to have the game pick a new number. Copyright © TechnoKids Inc. 54 TechnoPython | Python Session 3 Ask the Player to Guess the Number The player needs to guess the number. An answer typed into the program is stored as a string. It must be changed to an integer, which is a number. 5.  Ask the player to guess: #play game number=random.randint(1, 10) You need to change the print('The number is', number) input into an integer to be able to tell the player if pick=input('What is your guess? ') they are right or wrong. num_pick=int(pick)  Test the program. Type the SAME number that the game picked: GUESS IT GAME It's time for fun! Guess a number between 1 and 10. The number is 6 What is your guess? 6 >>  Close the Python Shell. Tell the Player if They are Correct The player needs to know if they are correct. This is done using the logical operators != different and == equals. Compare the random number with the player's guess. 6.  Tell the player if they are correct: #play game number=random.randint(1, 10) print('The number is', number) pick=input('What is your guess? ') num_pick=int(pick) if number!=num_pick: print('You are wrong.') if number==num_pick: print('You are right!')  Test the program.  Type the SAME number that the game picked. It should say You are right! GUESS IT GAME It's time for fun! Guess a number between 1 and 10. The game tells you the number. The number is 5 This is done to test the program. What is your guess? 5 You are going to move this line in You are right! the next step. >>  Test the program again. Pick a WRONG number. It should say You are wrong. The number is 3 What is your guess? 4 You are wrong. >> Copyright © TechnoKids Inc. 55 TechnoPython | Python Session 3 Move a Line of Code The game works! It tells the player if they are right or wrong. You no longer need at the start of the program the code, print('The number is', number). It was only there for testing purposes. 7.  Add a comment. Then move the code to the end of the game: #play game number=random.randint(1, 10) pick=input('What is your guess? ') num_pick=int(pick) if number!=num_pick: print('You are wrong.') if number==num_pick: print('You are right!') #game over print('The number is', number)  Test the program again. Can you guess the number? Loop the Game A while loop repeats a set of instructions. This will let the player play again. 8.  Make a variable called play. It will control the game play: #set up the game import random play=True  Create a loop. Indent each line of code to make it part of the loop: #play game while play: number=random.randint(1, 10) pick=input('What is your guess? ') indent num_pick=int(pick) 4 spaces if number!=num_pick: print('You are wrong.') TIP: To indent a block of text, select the lines of code. Press the if number==num_pick: TAB key on the keyboard. print('You are right!') #game over print('The number is', number)  Test the program. Can you guess the number? What is your guess? 5 You are wrong. The number is 2 What is your guess? 3 You are right. The number is 8 The game will NEVER end. You need to add code that will stop the loop. Copyright © TechnoKids Inc. 56 TechnoPython | Python Session 3 Ask the Player to Play Again A while loop repeats a set of instruction until a condition is met. In this game, the condition is whether the player wants to play again. If they do not, the loop stops, and the game is over. 9.  Ask the player if they want to play again: #game over print('The number is', number) If the player types anything other again=input('Play again? yes or no ') than 'yes', the else instructions run. if again=='yes': print("Let's play again!") play=False stops the loop and else: ends the game. play=False print('GAME OVER')  Test the program. First say yes you want to play again. Then say no. What is your guess? 2 You are right! The number is 2 Do you want to play again? yes or no yes Let's play again! What is your guess? 3 You are wrong. The number is 9 Do you want to play again? yes or no no GAME OVER Break the Game to Find a Bug The game breaks if the player types in a letter instead of a number! Find and fix the bug. 10.  Play the game again. Type k when asked to guess. What is your guess? k Traceback (most recent call last): The File "C:\Users\Student\Python\guess.py", line 15, in program cannot change a num_pick=int(pick) letter to a number. When it tries ValueError: invalid literal for int() with base 10: to, there is an error. 'k'  Create a variable that checks if the answer is a letter. If it is, then the game ends. pick=input('What is your guess? ') letter=pick.isalpha() pick.isalpha() checks if if letter is True: the answer pick is print('That is not a number.') alphabetical. It sets letter break to either True or False. num_pick=int(pick) Take the Coding Challenge 11. Pick from the options below to make your program even better:  Change the range of numbers the player can guess such as 1-25 or 1-50.  Be encouraging! Edit the words such as You are wrong!  Add a period to complete the sentence about the number: The number is 5. HINTS: Convert the variable number to a string: str(number). Use a plus sign + after the variable. Close Python Copyright © TechnoKids Inc. 57 TechnoPython | Python

Use Quizgecko on...
Browser
Browser