# Python Guessing Game

147

1

1

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.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Random Import Statement

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

## Step 2: Function Name

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.

## Step 3: Generate a Random Number

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.

## Step 4: Starting the While Loop

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.

## Step 5: User Input

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.

## Step 6: Exiting the While Loop of the User Guessed Correctly

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

## Step 7: If/else Statements

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

## Step 8: Try/except Statements

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

## Step 9: Congratulations

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!

## Recommendations

• ### Internet of Things Class

22,855 Enrolled