Introduction: Simple Python Number Guessing Game
In this tutorial we will be teaching how to create simple Python number guessing game in the Pycharm application. Python is a scripting language that is great for both beginners and experts alike. The coding style in Python is easy to read and follow. The end goal of this tutorial is to shine a little light on how to write a simple script for fun that may ignite one’s curiosity for programing.
Table of Contents:
1. Get Python version 3.7 and install
2. Get Pycharm and install
3.Setting up Pycharm for the first time
4. Creating a random number
5. Getting input from the user
6. Creating a basic while loop
7. Creating an "if", "elif", "else" statement
8. Displaying message to the user
Step 1: Overview
This guessing game was created in order to show a user some basic programming techniques using python with pycharm IDE. The basis of this tutorial will be using a random number generator to create a simple guessing game. The end result could lead to someone understanding how random numbers are generated. For instance, in video games where damage is dealt in numbers those numbers are generally generated with a random number generator that has specific requirements similarly to the one we generated. The random number generator can be more complicated, but one can get the basic idea of how it works.
Step 2: Installing Python and Pycharm
Step 3: Video Guide
Please watch the video guides above, and then check out the steps below to help you further understand how to write the game.
Step 4: Creating a Random Number
For the game to have a simple challenge we want to create a random number under 100. This number will be the one that needs to be guessed by the player. The number range will be between 1 and 99. We accomplish this by writing the following statement:
randomNumber = random.randint(1, 99)
"randomNumber" is a variable that we will store the random number in.
"random.randint(1, 99)" is used to generate a random number between 1 and 99.
*Note: Make sure at the top of the code you write "import random" or you will not be able to use "random.randint(1, 99)"
One important note is to follow the indentation exactly from the examples as Python is structured through indentation. If a statement is placed on the wrong indentation line the code may provide errors when one attempts to play the game.
Step 5: Getting Input From the User
In order for our game to work we must be able to recieve user input. We need to get guesses of what the random number will be from the player. The range for the number that can be guessed is from 1 to 99. This program does not provide an error when a number is outside of the range, however the loop will continue until the correct number is guessed.
We do this by using the command "input" which you can write like this.
guess = int(input("enter a number between 1 and 99: "))
We are storing the user input in a variable called "guess". The "int" means we are storing the input from the user as an integer meaning it will be a whole numerical value. The sections for input("enter a number between 1 and 99: ") tells the computer we are taking user input, and then display the following message if the loop continues.
Step 6: Creating a Basic While Loop
We must now create a while loop. To accomplish this, we need to write a statement that will operate until it is not true. The while loop is not indented in this program and includes the "If/Elif" statements that are indented below it. The "If/Elif" statements will continue to function until the while loop statement is not true.
while randomNumber != guess:
Step 7: Creating an If Elif Statement
The statement, "If/Elif" stands for if this is correct then do this if not, do something else. The statement is written so that the user can enter a new input if the initial guess is wrong. The print statement in the "If/Elif" will give them a hint to whether or not the guess is either too high or too low.
Step 8: Writing the Final Statement
The final statement is written on the outside of the while loop and with no indents. Once the user guesses the right number the while loop will "break" or "stop", and then move down to the final statement. This will occur once the variable "guess" and the variable "randomNumber" are equal. The game will then end until the program is started over again.
Step 9: Extras
After completing the guessing game Instructable, one may want to explore Python further. Here are a few ideas to challenge your Python prowess.
- Try change the number range for the random number.
- Change the messages to the user to something better.
Try make the program keep score of how many tries it to get the right answer.
Step 10: Keywords
Python is a programming language.
Pycharm is program that helps with making Python programs.
"random" is a random number generator
"variable" is a symbol for which the value may change
"int" is a data type short for integer whole numbers
"input" is how to take in user input
"print" will let you print a message on the screen for the user
"while" is loop statement that says do something while this is true
"if" a statement that means if this is true then do this
"!=" means not equal to
"<" means less than
">" means greater than
"elif" is short for the else if statement
Step 11: Entire Code
1 Person Made This Project!
- TeamOneUSF made it!