Python Guessing Game

by MariFran7 in Circuits > Apple

5540 Views, 4 Favorites, 0 Comments

Python Guessing Game

Screen Shot 2019-04-28 at 3.28.57 PM.png

The following instructions provide step by step guidance on how to write a program using the random module and creating a guessing game that interacts with the user. To get started you would need to have python installed on your computer and also open a separate file that you would write the code which you will eventually run in the python window.

Random Import Statement

Screen Shot 2019-04-22 at 8.29.48 PM.png

Before starting with the code for the program make an import statement on the top which says from random import * this will allow you to use some functions of the random module without having to right random in front of those functions

Function Name

Screen Shot 2019-04-23 at 12.08.10 PM.png

Start by creating the function name and then make it have one variable which will be the number that the random guess will stop at.

Generate a Random Number

Screen Shot 2019-04-23 at 12.13.15 PM.png

Next what we need to do is generate a random number from 1 to n with the randit function (where n is the number the user put in when calling the function) and store it into a variable.

Starting the While Loop

Screen Shot 2019-04-23 at 12.34.11 PM.png

Then make a while loop that stops when it’s False. So, assign done to False and then make the while loop to go until done isn’t False.

User Input

Screen Shot 2019-04-23 at 12.48.53 PM.png

When in the while loop make an input statement for the users and then store it into a variable. Make sure to turn what the user inputs into an integer.

Exiting the While Loop of the User Guessed Correctly

Screen Shot 2019-04-23 at 1.03.38 PM.png

When the user guesses the right number make them get out of the while loop by assigning done to True.

If/else Statements

Screen Shot 2019-04-23 at 1.05.04 PM.png

After that make if/elif statements that will print a message that is relevant to what the user guessed.

Try/except Statements

Screen Shot 2019-04-23 at 1.08.12 PM.png

Lastly, put a try/except inside the while loop that will print a message when the user inputs an invalid guess.

Congratulations

Screen Shot 2019-04-23 at 1.13.29 PM.png

You are now all done with the guessing game and you are ready to test it! This is how it’s supposed to work.

This was a program that interacts with the user and makes the guess a number until they get to the number that was randomly generated by the computer! Enjoy!