In this article, we will develop a Python-based number finding game together using the VSCode source code editor.
our project VS Code Since we will develop with VSCode, your computer must have a Python development environment ready. If your development environment is not ready from this article you can get help.
Beginner level Python knowledge is required while developing the project.
What kind of project will we develop?
In this article, we will develop a number game with Python. The program we have written will keep a random number in the specified range and the user will try to find this number with the least number of tries with the help of the clues given by the program.
So let's start!
First create a folder for your project:
$ cd ~/Documents $ mkdir number_puzzle
Open the folder you created with VSCode:
$ code number_puzzle
From the upper left corner of the VSCode window that opens File>New File Create a new document with:
Opened blank document Ctrl + S Save it as "number_puzzle.py" with the shortcut:
Let's install the Python plugin
I suggest you download a VSCode Python plugin to help us write our program. Open plugins from the left panel:
Type "Python" in the search field and search:
Click on the first plugin that pops up (with the most downloads):
If the plug-in is not installed, press install and wait for it to load, if it is already installed, you can proceed to the next topic.
Let's write our program
We go back to our number_bubble.py document to write our codes. Copy and paste the code I will give below directly into the document and save it. I will explain our code in more detail in the next topic.
import random lower_bound = 0 upper_bound = 100 wanted_num = random.randint(low_bound, high_bound) # take a random integer and save print( str(low_bound) + " to " + str(up_bound) + " (both included) I took number.\n" ) guess_num = 0 while True: user_guess = int(input("Your guess: ")) # we get guess_num from the user += 1 # we increase the number of guesses every time the user makes a guess if user_guess == sought_num: # user is correct guessed print( "\nCongratulations! " + str(guess_num) + " you found the number sought in the attempt.\n" ) break elif user_guess > guessed_num: # user's guess is greater than the number searched print("Searched number is smaller") elif user_guess < searched_num : # user's guess is less than the searched number print("The searched number is greater")
What did we write?
We added the random library that comes with Python to our program with the import command in the first line. We will use this library to generate random numbers.
lower_bound = 0 upper_bound = 100 wanted_num = random.randint(low_bound, high_bound) # take a random integer and save
We take a random integer from the lower_limit and upper_limit we have determined with the random.randint() method and save it to the sought_number variable. (including lower_bound and upper_bound)
print( str(low_bound) + " to " + str(up_bound) + " (both included).\n" )
When the program runs for the first time, we inform the user in which range we keep a number.
guess_num = 0 while True: user_guess = int(input("Your guess: ")) # we get guess_num from user += 1 # we increase the number of guesses every time user makes a guess if user_guess == number_sought: # user guessed correctly print( "\nCongratulations ! " + str(guess_num) + " you found the number searched in the attempt.\n" ) break elif user_guess > search_num: # user's guess greater than the searched number print("The searched number is smaller") elif user_estimation < searched_num: # user's guess is less than the searched number print("Searched number is greater")
This piece of code is the part where we take the guess from the user and evaluate it.
First, we create a variable called guess_num so that every time the user makes a guess, we increase its value by 1 and keep how many guesses the user has made until the user guesses correctly.
while True: We create an endless loop. Our goal here is to repeat the code we wrote until we break this cycle. If the user makes a wrong guess, repeatedly ask the user for their guess. It is very important not to forget to break this cycle, that is, to stop it, otherwise our code will run forever. We perform this cycle after the user guesses correctly and informs him that he has guessed correctly.
break We break it with the command.
Let's run our program
Open a terminal and navigate to the number_puzzle.py document where we wrote the code:
$ cd ~/Documents/number_puzzle
Run the Python program we wrote:
$ python3 number_puzzle.py
For the continuation of the Python series this article You can read.