A GUESSING GAME — Series 3: Learning Python Through Projects #PythonBeginnerSeries
Do you love playing games? I do too! In this project, we will be building a simple guessing game in python as well as explore new concepts — ‘Flow control statements in Python’. This project will be demonstrated using the Default Python IDLE. However, you can use any text editor of choice.
As always, we will get into building the project first, then explain the concepts afterward.
Ever heard the saying ‘You learn how to cook first, then learn the ‘art of cooking’ later on’. Yes! Cooking is an art.
THE WORD GUESSING GAME IN PYTHON
STEPS:
- Open a new file
- Save as ‘guess.py’
- Create a variable and call it ‘secret word’.
- Assign the variable to any secret word of choice. I will call mine ‘Python’.
- Create a ‘guess variable’ using the input statement to prompt a user to make a guess.
6. Now, create a condition using the ‘while keyword’ such that, for as long as the user’s input is wrong, the game does not end. This is done as follows:
- Define the while keyword
- Create a condition using a comparison operator (not equal to, !=). Use the comparison operator to check if the user’s entered input is the same as the stored secret word. Use a semicolon(:) to indicate the presence of ‘a block of code’.
- Print a message beneath the while statement(using four spaces before the print statement) for if the guess is incorrect. See the image below for clarity.
- Also re-create the guess variable, using the same indentation as the print statement. This time use a different message.
7. Finally, include the last print statement to output a congratulatory message for when the user finally guesses the word correctly.
Take note of the indentation. I will explain soon how these work in while.
8. Finally, run the program to get the game working.
Great! The program works fine. Now let’s move into explaining the concepts.
PYTHON CONCEPTS USED
So we are already familiar with how to assign variables and how to use the input and print statements. If you have not seen the previous articles where these concepts were explained, I have included the links at the end of the article.
- The ‘while keyword’: This is a flow control statement keyword in Python. Keywords are Python’s reserved words, these words cannot be used as variable names, function names, or identifiers in Python. They have special meanings and are used for specific tasks in Python. Because Python is a case-sensitive programming language, these words are also case sensitive, they have to be called just as can be recognized by Python. For example, ‘While’ is different from ‘while’, ‘True’ is not the same as ‘true’ et cetera. The same rule applies to variable names as well, ‘Guess’ is not the same as ‘guess’, in Python. For a more understanding, I have included links to useful resources below.
So in Python, Flow control statements are written blocks of codes in an ordered form of instructions. They are basically used for creating conditions and making decisions in Python. It is similar to how we also have a structure for making decisions or carrying out tasks. For example, When you wake up in the morning, You have a sequence of tasks you do to start your day, and you do these tasks repeatedly everyday ( A Loop). Sometimes, you set conditions for when an event occurs or not: “If it rains, I will take the bus, else it’s sunny I will walk. This is a simple decision-making process, where you set a condition for if it rains or if it’s sunny. It is also how computers make decisions. These sets of instructions are written as a block of codes(i.e embedded codes to achieve a particular task). Teaching computers how to make decisions by themselves is the point of programming after all.
There are three(3) types of Flow control statements in Python: “Conditional Statements, Iterative Statements, and Transfer Statements. In this article, the focus is on iterative statements. In the next series, we will create a project using conditional statements alongside the iterative statements.
Iteration simply means going through a process over and over again for as long as a certain condition is True(just like an everyday routine). In programming, Iterative statements are used to repeat a block of code a number of times given a certain condition.
There are two types of iterative statements in python.
- While loop — using the ‘while’ keyword
- For loop — using the ‘for’ keyword
The project we built earlier made use of just the while loop.
The difference between the while and for loop is that, The while loop keeps running as long as a condition is true(just like the project built earlier, the program kept running for as long as the guess was wrong), Whereas in the for loop the computer executes the command for a fixed number of times. We will explore this in the upcoming series.
To create an iterative statement, we start with a keyword(either the while or for), a semicolon ‘:’, then the condition, and finally, an action to be carried out.
Let’s take a look at the while statement used in the project earlier:
The block of code was defined using the while keyword, indicating an iterative process, a condition was created using a comparison operator(!=, not equal to), finally, an action was created below(print(‘Oops! Wrong guess. Try again’) and a prompt for trying again(guess= input(‘Try a new guess: ’)).
Note that while the first print statement was defined inside the while loop block alongside the guess variable, the last print statement was defined outside the while loop block of code. This is because the last print statement will only get executed when the loop ends(when the iterative process is over) and the condition no longer holds true(guess is correct). Notice that when you run the program, the final statement only gets executed when the correct word is eventually entered.
- Comparison operators: So we also explored the use of comparison operators in building the program. Comparison operators are quite different from Arithmetic operators in Python just like any other programming language. In programming, comparison operators are used in comparing two values or to check if a condition is True or False.
The table below shows the Comparison operators in Python and how to use them.
Fun fact: Do we remember this ‘=’ operator? So while the equal to ‘=’ is used for variable assignment in Python, ‘==’ is used as a comparison operator. They both belong to a different class of operators in Python.
- Block of Codes in Python: Generally, in programming, a block of code indicates a sequence of action or an event to be carried out as a unit. It is a form of instruction for a computer to carry out a set task. The codes are embedded in such a way that it forms a structure. In Python, this is usually created using the right keyword, a semicolon, and four indentation spaces before the start of a new line of code. This indentation is just an indication that whatever is written afterward should be recognized as one unit and not separate entities. The general convention is to use four or five spaces in python. Although, when using certain text editors, indentations are automatically created by the program once the right keyword and a semicolon are used.
END NOTE
Congratulations! You just built your third Python project. I hope you learned something new.
Please leave a clap and share.
Look up links to further resources below. Also find below the link to the Github repository for the code used in this project, as well as links to some other exercises I hope you will find quite useful.
LINKS TO FIRST TWO SERIES
GITHUB REPOSITORY LINK
FURTHER RESOURCES
CONNECT WITH ME ON SOCIAL MEDIA
LinkedIn: https://www.linkedin.com/in/aminah-mardiyyah-rufa-i
Twitter: @diyyah92