You will practice the main concepts of O.O.P, and how to use them in a fully functional application. Python is a multiparadigm programming language, which means that we as developers can choose the best option for each situation and problem. When we talk about Object-Oriented Programming, we are referring to one of the most used paradigms to build scalable applications, in the last decades.

The basics of OOP

We are going to take a quick look at the most important concept of O.O.P in Python, the classes. A class is a template in which we define the structure and behavior, of objects. That template allows us to create Instances, which are nothing but individual objects made following the composition of the class. A simple book class, with the attributes of title and color, would be defined as follows. If we want to create instances of the class book, we must call the class and pass arguments to it. A good representation of our current program would be:

The awesome thing is that when we check the type of the blue_book and green_book  instances, we get “Book”. After having these concepts crystal clear, we can start building the project 😃.

Project statement

While working as developers/programmers, most of the time isn’t spent writing code, according to thenewstack we only spend a third of our time writing or refactoring code. We spent the other two-thirds reading other’s code and analyzing the problem we are working on. So for this project, I’ll generate a problem statement and we will analyze how to create our app from it. As a result, we are making the complete process, from thinking about the solution to apply it with code. I know that the requirements maybe a little bigger, but I promise you that we will solve them in this article 😁. The game must have a lives and a points system, where the student starts with 3 lives and must reach a certain amount of points to win. The program must show a “lose” message if the student depletes all his/her lives. The game must have two modes, random multiplications and table multiplications. The first should give the student a random multiplication from 1 to 10, and he/she have to answer correctly to win a point. If that doesn’t ocurr the student lose a live and the game continues. The student only wins when she/he reaches 5 points. The second mode must display a multiplication table from 1 to 10, where the student must  input the result of the respective multiplication. If the student fails 3 times he/she loses, but if she/he completes two tables, the game finish.

Divide and conquer

The most important skill in programming is problem-solving. This is because you need to have a plan before starting to start hacking in the code. I always suggest taking the bigger problem and dividing it into smaller ones that can be both, easy and efficiently solved. So if you need to create a game, start by breaking it into the most important parts of it. These sub-problems will be much easier to solve. Just then you can have the clarity of how to execute and integrate everything with code. So let’s make a graph of how the game would look like.

This graphic establishes the relations between the objects of our app. As you can see the two main objects are Random multiplication and Table multiplication. And the only thing they share is the attributes Points and Lives. Having all this information in mind, let’s get into the code.

Creating the Parent game class

When we work with Object-Oriented programming, we search for the cleanest way to avoid code repetition. This is called DRY (don’t repeat yourself). Note: This objective isn’t related to writing the fewer lines of code (Code quality mustn’t be measure by that aspect) but to abstract the most used logic. According to the previous idea, the parent class of our application must establish the structure and desired behavior of the other two classes. Let’s see how it would be done. Wow, this seems a pretty huge class. Let me explain it in deep. First of all, let’s understand the class attributes and the constructor.

Basically, class attributes, are variables created inside the class, but outside of the constructor or any method. While instance attributes are variables created only inside the constructor. The main difference between, these two is the scope. i.e class attributes are accessible both, from an instance object and the class. On the other hand, instance attributes are only accessible from an instance object. Another article can dive deeper into this topic. Stay in touch to read it. The get_numeric_input function is used to prevent the user from providing any input that isn’t numeric. As you may notice this method is designed to ask the user until it gets a numeric input. We will use it later in the child’s classes.

The print methods allow us to save the repetition of printing the same thing each time an event occurs in the game. Last but not least, the run method is just a wrapper that the Random multiplication and Table multiplication classes will use to interact with the user and make everything functional.

Creating the child’s classes

Once we’ve created that parent class, which establishes the structure and some of the functionality of our app, it’s time to built the actual game mode classes, by using the power of inheritance.

Random multiplication class

This class will run the “first mode” of our game. It is going to use the random module of course, which will give us the ability to ask the user random operations from 1 to 10. Here is an excellent article about the random (and other important modules) 😉. Here is another massive class 😅. But as I stated before, it’s not the numbers of lines it takes, is how much readable and efficient it is. And the best thing about Python is that it allows developers to make clean and readable code as if they were talking normal English. This class has one thing that may confuse you, but I’ll explain it as simply as possible. The constructor of the child class is calling the super function which, at the same time refers to the parent (BaseGame) class. It is basically telling Python: It is not necessary to put self, inside the  super().init() part just because we are calling super inside the constructor, and it would result in redundant. We also are using the super function in the run method, and we will see what’s happening in that piece of code. As you may notice the run method in the parent class, prin the welcome and description message. But it is a good idea to keep that functionality and also adding extra ones in the child classes. According to that, we use super to run all the code of the parent method before running the next piece. The other part of the run function is pretty straightforward. It asks the user for a number with the message of the operation he/she must respond. Then the result is compared with the real multiplication and if they are equal, adds a point, if they don’t take off 1 life. It’s worth saying that we are using while-else loops. This exceeds the scope of this article but I’ll publish one about it in few days. Finally, get_random_numbers, uses the function random.randint, which returns a random integer within the specified range. Then it returns a tuple of two random integers.

Random multiplication class

The “second mode”, must display the game in a multiplication table format, and make sure the user answers correctly at least 2 tables. For that purpose, we will use again the power of super and modify the parent class attribute points_to_win to 2. As you can realize we are only modifying the run method of this class. That’s the magic of inheritance, we write once the logic we use in multiple places, and forget about it 😅. In the run method, we are using a for loop to get the numbers from 1 to 10 and built the operation that is shown to the user. Once again if the lives are depleted or the points needed to win are reached, the while loop will break, and the win or lose message will be displayed. YEAH, we created the two modes of the game, but until now if we run the program nothing will happen. So let’s finalize the program by implementing the mode choice, and instantiating the classes depending on that choice.

Choice implementation

The user will be able to choose what mode wants to play. So let’s see how to implement it. First, we ask the user to choose between the 1 or 2 modes. If the input isn’t valid the script stops running. If the user selects the first mode, the program will run the Random Multiplication game mode, and if he/she selects the second, the Table multiplication mode will be run. Here is how it would look like.

Conclusion

Congratulations, you just build a Python app with Object-Oriented Programming. All of the code is available in the Github repository. In this article you learned to :

Use Python class constructors Create a functional app with OOP Use the super function in Python classes Apply the basic concepts of inheritance Implement class and instance attributes

Happy coding 👨‍💻 Next, explore some of the best Python IDE for better productivity.

Build a Python Multiplication Table App with OOP - 87Build a Python Multiplication Table App with OOP - 86Build a Python Multiplication Table App with OOP - 11Build a Python Multiplication Table App with OOP - 82Build a Python Multiplication Table App with OOP - 63Build a Python Multiplication Table App with OOP - 51Build a Python Multiplication Table App with OOP - 65Build a Python Multiplication Table App with OOP - 6Build a Python Multiplication Table App with OOP - 22Build a Python Multiplication Table App with OOP - 93Build a Python Multiplication Table App with OOP - 68Build a Python Multiplication Table App with OOP - 42Build a Python Multiplication Table App with OOP - 64Build a Python Multiplication Table App with OOP - 57Build a Python Multiplication Table App with OOP - 75Build a Python Multiplication Table App with OOP - 46Build a Python Multiplication Table App with OOP - 41Build a Python Multiplication Table App with OOP - 56Build a Python Multiplication Table App with OOP - 37Build a Python Multiplication Table App with OOP - 31Build a Python Multiplication Table App with OOP - 4Build a Python Multiplication Table App with OOP - 50Build a Python Multiplication Table App with OOP - 12