Simple, Quick, and Fun Number Guessing Python Game for Beginners

by Luhboy in Circuits > Software

2175 Views, 34 Favorites, 0 Comments

Simple, Quick, and Fun Number Guessing Python Game for Beginners

pythonPhoto.png

Hello! Welcome to my tutorial on how to build a simple number guessing game within python. This is aimed towards people who already have python installed and have very minimal experience. This short code is designed to be a fun activity any beginner can follow. Not only is it relatively simple, but it is also a fun game you can show to your friends and family as well! If you have any amount of Python experience, you should know that correct syntax and capitalization is very important within your code. If you run into any errors when running the code, the first thing you should do is check for any typos or missed characters. This code should take the average person around 10 minutes to complete, Good Luck!

Supplies

For this activity you don’t need much:

  1. A computer or laptop
  2. Minimal python knowledge
  3. Python

Creating a New File

step1.png

Open and save a new file within python. If you are unsure of how to do this, follow the link to the quick tutorial: https://youtu.be/Kn1HF3oD19c.

Import Random

Picture2.png

Type your first line of code: “import random.” This will import premade functions into your Python shell, allowing for randomly picked numbers.

Defining Your Function

step3.png

Define your function. This name can be whatever you want, however I will be calling it “main.” The syntax when defining a function within python is very important. For example, if I were to leave out the semicolon or parentheses, the entire code would fail and you would see the invalid syntax error message. 

Define Your Variables

step4.png

Define all your variables. For this game we will need a variable for the user’s guess, an answer variable that randomizes the game each time, and a guess counter variable that keeps track of the user’s attempts. I will choose the number 100 as my max, however for your code you can go as high or low as you want. The guess variable must be equal to a number to avoid any error codes. Lastly, the guessCount must equal 0 to get an accurate count of the users guesses. The str(random.randint(1,100)) line of code is randomly picking a number between 1-100 and turning it into a string to make it comparable to the user’s guess.

Create Your While Loop

step5.png

Create the while loop for your game. This loop must continue as long as the user’s guess is not equal to the answer, or the word “quit”. As soon as the user types in the correct number or the word “quit” the loop will break.

Request a Guess From the User

step6.png

Ask the user for their guess by making the guess variable equal to an input function, requesting a number to be typed in. Their submitted number will then be assigned to the variable guess.

Increase Your Counter Variable

step7.png

Make the guessCount variable equal to itself plus 1. This will add 1 to the user’s guess counter through every iteration of the loop.

Create Your First If Statement to Compare the Guess to the Answer

step8.png

Create the first if statement. This if statement will be for the circumstances where a guess is higher than the answer. This will also include a print statement telling the user their guess was too high.

Create an Elif Statement to Compare the Guess to the Answer

step9.png

Create the first elif statement. This elif statement will be for the circumstances where a guess is lower than the answer. This will also include a print statement telling the user their guess was too low.

Create If Statement for Guess Counter

step10.png

Create your next if statement. This if statement will check if the user’s guess count is above 10 tries. This will also include a print statement telling the user they can quit the game at any point by typing “quit”.

Create Another If Statement for Guess Counter

step11.png

Create your next if statement, checking if the user’s guess counter has passed 20. The break function will make it so that on the 21st run through of the while loop, the loop breaks, limiting the user to 20 tries. The guess counter amounts can be changed if you wish to for your code.

Create an If Statement to Compare the Guess to the Answer

step12.png

Create another If statement (the first one outside of the loop). This If statement will print a congratulations message if the user’s guess is equal to the answer. The double equal signs are used to compare rather than assign a value, with one sign.

Create an Elif Statement to Compare the Guess to the Word "Quit"

step13.png

Create an elif statement. This elif statement will print a goodbye message if the user inputs the text “quit”.

Create an Else Statement for Exceeding Number of Tries

step14.png

Create an else statement in which it prints “Number of tries exceeded” if the other two conditions are not met.

Run Your Code

step15.png

Run your code by recalling your function name at the end. Then either click Run and then Run Module, or press F5.

Congratulations on completing this short and fun Python game! I hope it was not too difficult for you to follow along. Try it out a few times for yourself to see how fast you can guess the secret number, and even show off your code by challenging your friends to see who can guess the number in the least amount of tries! If any problem arises, comment down below and I will do my best to help you out.