Beginners Python Project: How to Make a Calculator?

Hello world 👋🏻 my name is Francisco, fcoterroba on the Internet and today I bring you a post in which I am going to explain to you, as I always do, explaining line by line, how to program one of the most basic projects in any programming language, a calculator!

Before we start racking our brains 🧠 to program this game, I remind you that a couple of weeks ago I uploaded a video to my YouTube channel in which I explain how to install a smart light bulb 💡 with which you can change color, turn it off or on with your voice and/or mobile phone, reduce or increase its intensity, etc. Plus, you have it ready in LESS than 3 MINUTES ❗ In the description you also find some little surprises 😏
You can watch (and subscribe) here 👇🏻

Before you start programming with Python, you need to have it installed correctly on your operating system. For this I recommend you read the official documentation available here.

Now yes, let's start!

GAME IN THE COMMAND LINE

Firstly, I want to show you in a simple gif, what you are going to end up doing when you finish reading this post! 👇🏻

You like it, right? Well, keep reading! 👀

The first thing we should do is open our favorite IDE (in my case it is Visual Studio Code) 💙

Subsequently, we must create the two variables that, a priori, we will know we are going to need, which are the variables to store the numbers that the user will insert.

Although I say it every time I make a post about Python, I remember that in this programming language it is not necessary to specify what type the variable will be (Char, float, integer,...), unlike many other programming languages since, Python 🐍, it will automatically understand the type of variable depending on what we save in it.

For this project (and for almost all of them, really) I like to import both the package time (import time) Since, I like to set a few seconds to make it easier for the user to read the screen. Its use is simply writing time.sleep(x) changing the x by a certain number, in seconds.

Comments in Python are written by putting the symbol at the beginning of the line #

With print, as in almost all programming languages, we are going to write on the screen ✍🏻.

Well, after explaining all that previously, commenting on a few lines of code, importing the necessary packages, preparing the first function and making a presentation to the program, we will have something like this:

#Calculadora en CMD by fcoterroba.com 
import time

print("¡Hola! Bienvenido a la calculadora escrita en Python y desarrollada por fcoterroba")
time.sleep(1)
print("Puedes encontrar más códigos tan interesantes como este en mi página web")
print(f"www.fcoterroba.com\nAhora sí, comencemos: ")
time.sleep(0.8)

With this we will have created a very fast, interesting and minimalist main screen.

We now need to ask the user to write the two numbers on which they will want to work with addition, multiplication, subtraction,...

To do this we must create the name of the variable followed by an input, converted to int, since, by default, the input collects values and stores them in text format.

numero1 = int(input("Dime el primer número: "))
numero2 = int(input(f"Bien! Ahora dime el segundo número: "))
print(f"De acuerdo, has escogido el {numero1} y el {numero2}")
time.sleep(1)

Now that we have the numbers saved in variables, we must ask the user what type of calculation they want to perform between them and, finally, perform a series of yes/else conditions to find out the key pressed.

simbolo = input(f"¿Qué quieres hacer con estos números? (Escribe la primera letra)\n -Sumar\n -Restar\n -Multiplicar\n -Dividir\n")

if simbolo == 's' or simbolo == "S":
    print(f"{numero1} + {numero2} =",(numero1+numero2))
elif simbolo == 'r' or simbolo == "R":
    print(f"{numero1} - {numero2} =",(numero1-numero2))
elif simbolo == 'm' or simbolo == "M":
    print(f"{numero1} * {numero2} =",(numero1*numero2))
elif simbolo == 'd' or simbolo == "D":
    print(f"{numero1} / {numero2} =",(numero1/numero2))
else:
    print(f"No has escrito ninguna letra correcta\nS|s para sumar R|r para restar M|m para multiplicar o D|d para dividir")

And, we're done!

Now, to you, little friend, I propose two points to complete in this project:

  • First, you have to improve the calculator that we have made, in the sense of code organization, have clean code and not be so much spaghetti code 🍝. You have to use functions, place the code correctly in its place, etc.
  • Finally, you need to port this calculator to a graphical interface or GUI. The most used in Python 🐍 is TKinter, but you can use any other package you want such as PySimpleGUI, Kivi, PySide2, etc. The options are wide!

And that's all for today guys, I hope you liked the post, approaching programming easily and simply. Don't forget to follow me on TwitterFacebookInstagram and LinkedIn. See you next week!