Making a simple calculator in Python! - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Friday 5 March 2021

Making a simple calculator in Python!


Hey all! This is the third tutorial of our Fun Friday series. Today, we are going to make a simple calculator in python. I hope you will love it!


In a hurry? Just read the chronology of the program given at the end of this article!


This program is going to be a really basic one in which we will be using math functions like additionsubtraction, multiplication and division to make our calculator.


As always you can find the final code at the end of this article. But I would advice you to stick with us this whole time as we are going to go through each and every step you need to follow to become the best programmer in the world!


Excited? Let's start with the overview!


Overview:


In this program, we would be making a simple calculator to perform basic math functions with two numbers. Remember that this is just a beginner's tutorial to mathematical operations in python.


Our first task would be to take the two numbers from the user. Specific numbers would be assigned to the four mathematical operations. So to run any operation, the user needs to enter that specific number.


To make it simple, we won't be using functions. Hence, to check what user wants, we can run an if-else statement.


*As I have already told you some of the basics of the program, you can try to write the code on your own. See if the solution matches!*


Understanding what we need to do:


Here I will tell you step by step what you need to do.


Step 1: Asking the user for the two numbers as input


This is the easiest part of the program. We have to ask the user to enter the two numbers he/she wants to perform calculations with. Also, don't forget to typecast the input into integer using the int function.


I have stored these numbers in variables num1 and num2 respectively.


Here's the code for this step:


Step 2: Ask the user for the operation he/she needs to perform


To do this, what we have done is that, we have assigned each operation (add, sub, mul, div) a specific number (1, 2, 3, 4 respectively). 


We would have to print this guide first and then ask the user to enter the desired number. Again, the input would have to be type casted to  integer using the int function.


I have stored this input in the variable func.


Here's the code for this step:


Step 3: Perform mathematical operation as per user input


After the user inputs the desired function, the program still doesn't know what the user wants to perform. Hence, here we would use the if function.


Then we have written some if-elif statements inside which there are simple commands to do basic functions add, sub, mul, div corresponding to their specific number. 


When user enters the specific number for an operation, then the code inside the corresponding if statement runs and the output is printed using the print command.


Also, if option entered by the user matches no operation, then the else statement runs and asks the user to give the correct input.


Here's the code for this step:


So we have successfully found the best way to make a simple calculator!


Here's the chronology of the program:


  1. User enters the two numbers. 
  2. User is given specific numbers corresponding to operations. 
  3. User enters the required number.
  4. The if statement corresponding to that particular number runs and the basic calculation code inside it executes.
  5. If the option entered matches no if statement, then else statement runs and asks the user to enter the correct input. 

I hope that you are able to understand how the program works. If you are still facing issues, then just try playing with a code. That will help you to understand the importance of each and every line. Otherwise, the comments section is always open for you queries!


Here's the final code:


Here's the output of the code:

Making a simple calculator in Python!

Making a simple calculator in Python!


This is the end of our amazing journey where we learnt the logic behind making a simple calculator in python. 


If you liked this tutorial, then don't forget to share it with your friends as this may help someone struggling in coding!


Also, don't forget to subscribe to our newsletter as we do these amazing coding sessions each week.


Again thanks for reading!

No comments:

Post a Comment