In this article and video, you will learn how to write a simple Guess-the-number game in Python using a normal text editor.
This tutorial is meant to be an easy Python project for beginners, so don’t worry if you don’t understand everything at first. The main point is to see that code is just text.
You can watch the video tutorial below, and/or continue reading this blog for the written tutorial. Now let’s go build and run your first interactive Python project!
- Video Tutorial: Python project for beginners
- Install Python on your computer
- Build the Game
- Play the Game
- Parts of a Python project
- Tutorial take-aways
If you don’t already have Python installed on your computer, visit Python.org to download it. This tutorial is taught for UNIX-based systems (Mac & Linux). If you are on Windows and you don’t have Python pre-installed, you can also build the game in an online coding interface, such as repl.it.
The first step in building your Python project for beginners game is to write your code in a text editor.
Open up any Text Editor – this can be as simple as the built-in TextEdit program on MacOS:
Code is just plain text. So, if you’re using TextEdit, you can press Cmd+Shift+t to switch to plain text. Doing so means that you can’t apply any formatting, such as bold or italics. Remember that code is just text, so you won’t need any formatting for it. Your window should look like this now:
Now it’s time to write some code! Type the following code into your TextEdit file:
import random num = random.randint(1, 10) guess = None while guess != num: guess = input("guess a number between 1 and 10: ") guess = int(guess) if guess == num: print("congratulations! you won!") break else: print("nope, sorry. try again!")
Make sure you type the code exactly as you see it above, including the 4 spaces for indentation. You can also copy-paste the code from this online resource. Your text window should look like this, which is already the full code for this Python project for beginners:
Finally, let’s save your text file using the python file extension
Press Cmd+s or go to File/Save and save it on your Desktop with the name
And that’s it for writing the code. Next step is to run the code and play your game.
To run your Python project on MacOS, open up your Terminal. Press Cmd+Space to open up Spotlight, and type Terminal, then press Enter:
This will open up your Terminal, a tool that programmers use on a daily basis. If you join one of our courses, you will get to know your Terminal in much more detail, but for this beginner project you don’t need to worry about it too much. Just type the following in there:
This will teleport you to your Desktop, where you saved the
guess.py file that contains your code-text.
Now you finally get to play your guess-the-number game. Since you wrote it in Python, you need to also start it using Python. In your terminal, type the following and press Enter:
And lo and behold! Here you are! You’ve officially built and run your very own Python project for beginners game! Now you are ready to play.
If you want to play again after it finished, you can press the up arrow once and your terminal will show you the previous command:
Pressing Enter will start the program again from the beginning.
There are a lot of different concepts that went into creating even this simple Python project for beginners. Let’s take a look what they are:
In the screenshot above you can see the filename of the Python file you created, as well as the code saved you saved in the file. Now let’s dive deeper into the code and learn about which programming concepts you touched upon by making this file.
Fair warning : There’s a lot going on! Much like a paragraph of English, a script can be broken into many parts: an introductory sentence, references to outside text, subjects, nouns, verbs, and sometimes even new vocabulary.
Keep in mind that, like a paragraph of English, if you understand these parts, then it’s likely that you can write and understand another similar paragraph. Even if you haven’t seen it before. It also takes some training before you will be able to do that, so don’t feel overwhelmed if you won’t grasp it all right away. For now enjoy this depiction of the cool code you wrote!
We know that is a lot of parts with a lot of colors! If you’re intrigued to learn more about writing your own Python projects, check out CodingNomads Python Programming course. With some time and effort, you will soon be able to grasp these concepts and start speaking the coding language too!
- Code is just Text : Programming is just writing text, and all you need is a simple text editor to get started!
Run using Python : After writing your code text, you run Python programs with
- Python project components : Yes, code is just text… but is it also code There are many parts of a Python project, but once you get a grasp you open the door to creating any Python project of your own.
Congratulations again on building your first Python project for beginners! If you’re interested to learn more Python coding, click below to check out CodingNomads’ Python Programming Course, and Python Career Track Course.
The post Python Project for Beginners: Guess-the-Number Game first appeared on CodingNomads.