"Guessing a randomly selected number"
On Python
By Arish Sharma
Edition 14 is the last edition of the 2025-26 academic year. See you next year!
Python is a beautiful and one of the easiest programming languages to learn and use. Python is used in almost everything, for example, it is used in websites to code robots and AI. To get started with Python, you must have any type of programming software installed on your computer that supports Python such as VS Code, Notepad++, etc. If you do not have Python, then you must follow a tutorial on how to (here are many tutorials on the internet).
Let’s get started with the game!
Before we get Started:
First, you must name your file and save it. This makes the file easy to locate through your computer. It must have ‘.py’ at the end to make sure the file supports your Python code.
Step 1:
The first step is to type this into your file. This line of code is the main pillar supporting our game and it must be typed correctly. As shown here, the ‘import’ function imports the ‘random’ module and the ‘time’ module. The ‘random’ module randomly generates a number within what it is being used for (in this instance, in this game) and the ‘time’ module is for the visual effects (in this instance, in this game)
Step 2:
The next line of code should be this. The in-built print function says out the text provided within its brackets. The quotation marks within the brackets shows python that the sentence is a ‘string’. The word ‘string’ in the coding world refers to ‘text’ in which the computer understands.
Step 3:
These are variables.Variables are used to store value in them, just like how containers work. The ‘=’ refers to the variable storing whatever the programmer stores. The ‘input()’ function refers to the function that is going to ask the user to enter a value, and then store it into a variable. But what we need for the game is for the user to enter only ‘integers’; Not decimals, but only whole numbers.
So that’s why we put the ‘input()’ function inside of the ‘int()’ function so that the game stops functioning and needs to be run again if the user doesn’t put an integer in it.
Lastly, the variables ‘firstnum’ and ‘lastnum’ refer to the range that the game is gonna determine where it’s gonna place a random value for the user to guess.
Step 4:
Here, we write ‘time.sleep(1)’. We extract the ‘sleep()’ function from the ‘time’ module. ‘time.sleep(1)’ means that the game is going to pause for 1 second and then continue working again. This is purely for the effect because it looks kind of cool. The ‘print()’ function is gonna say as an output “Let the games begin!”.
Step 5:
The variable above uses the ‘random’ module in which the variable is extracting a specific function from the module called ‘randint()’. Since the ‘random’ module is an in-built file provided by Python, we extract the ‘randint()’ function in which we put the variables inside it’s parentheses so that the function decides where a random number is going to be within the range.
Step 6:
Here comes the fun part! We make a variable ‘guess’ in which it stores the value that the user provides to guess the random number the computer makes.
Then we make a loop where it uses the command ‘!=’ . This means that if the variable ‘n’(the computer decided number) it doesn’t equal the user’s input; It’s going to follow whatever the programmer has coded as long as the user doesn’t guess the number right.
The while loop is like a box where the if statements go; The first if statement says ‘if the guess is lower than the computer decided number’ then it should say ‘Too Low!’ and ask to try again. The ‘elif’ afterwards stands for ‘else if’ where if the user’s guess is higher than the computer decided number, it should say ‘Too high!’ and ask to try again.
Now if the user correctly guesses the computer decided number, then we have the ‘else’ statement where we put ‘break’ to say to cancel the code and then say that the user has guessed it right.
A thing to notice here is that there should be indents within the code structure as shown above. A way to visualise it is like we have a box where we have to put things in it, that’s what indents are for and they are to tell the computer that those values belong inside the box and not outside of it! So therefore, the contents within the loop should be indented and after the user has guessed it right, the loop ends and then it says ‘You Guessed It Right!!’ to end the game.
Conclusion
Finally, your game’s code should look like this or if you followed all the instructions correctly then it should look exactly like this and should function the way it says. You could also add how many times it took for the user to correctly guess the number but that’s when you have developed a deeper understanding of how to code in Python.
Here are some websites that personally helped me better understand Python:
https://www.w3schools.com/ (W3schools is a website that teaches you how to code in any language you want with it’s interactive playgrounds and simplified explanations).
https://www.geeksforgeeks.org/ (GeeksforGeeks is a website that teaches you how to code the most popular coding languages in the world with it’s interactive playgrounds, deeper understanding of the coding works, challenges and coding interview preparations).
https://stackoverflow.com/questions (Stack Overflow is a forum that people ask and answer questions about their code and how to improve it).
https://scratch.mit.edu/ (Before I learned how to code using Python, I learned to visualise it using Scratch. Scratch is an MIT built educational website where you code using blocks to make games and programs, a must for each programmer curious to code).
https://www.youtube.com/c/programmingwithmosh (Programming with Mosh taught me how to code in the best way possible. I’d highly recommend to watch his videos on whatever coding language you want to pursue since his tutorials helped me a lot in my coding journey)
Bibliography
ABCya! “ABCya! • Educational Computer Games and Apps for Kids.” Www.abcya.com, 2025, www.abcya.com/games/guess_the_number. Accessed 16 Nov. 2025.
GeeksforGeeks. “GeeksforGeeks | a Computer Science Portal for Geeks.” GeeksforGeeks, 2024, www.geeksforgeeks.org/. Accessed 16 Nov. 2025.
Staff, Coursera. “Python vs. C#: Which Language Is Best for AI?” Coursera, 2024, www.coursera.org/articles/python-vs-c-sharp. Accessed 13 Nov. 2025.
The CTO Club. “20 Best Coding Software for Windows in 2024.” The CTO Club, The CTO Club, 13 Aug. 2024, thectoclub.com/tools/best-coding-software-for-windows/. Accessed 13 Nov. 2025.
W3Schools. “W3Schools Online Web Tutorials.” W3schools.com, W3Schools, 2025, www.w3schools.com/. Accessed 16 Nov. 2025.