Hello and welcome to all my lovely readers in our articles. As you all are now used to with our site, I believe our articles help you a lot in your day-to-day coding life. Isn’t it so? Please share your thoughts at firstname.lastname@example.org so that we can improve ourselves better and better in terms of your expectations.
Well! So let’s jumps into today’s topic. This is yet another interesting and very very basic python example we will discuss today.
Wait! Have you checked out our previous articles in this series? Not yet? My recommendation is to just have a quick look. You will get a clear understanding of Python basic examples. Our last popular articles are Python Examples For Beginners and Python Examples For Beginners| Check Prime Number.
Today we will create a mini calculator in Python. For that we can split the steps as follows:
Open your favorite code editor and type the below code.
In the above portion of the code, we will first offer the user to choose which operation they would like to do? Like Addition, Subtraction, Multiplication, Or Division. Based on user choice we will ask the following:
# Take input from the user
choice = input("Enter Your Choice(1 OR 2 OR 3 OR 4): ") # Check if choice is one of the four options
if choice in ('1', '2', '3', '4'):
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
calculator(num1, num2, choice)
Looking complicated? Not at all! Let’s see what we are going to do here. Our aim is to get the choice from the user like 1, 2, 3, or 4. Once we know what the user going to do we will further ask him/her to input 2 numbers like the first number and the second number. And then we will send them as a parameter in a function called calculator. Don’t worry this function is built by us it’s not at all a system function so you are free to write anything inside this function. Ok. So now you might be a wonder that we are not talking about the while loop we are using here and the if condition. Don’t worry, let’s see why this has been used here.
Imagine you first give the choices to your users to choose what they will do like Addition, Subtraction, Multiplication, Or Division. Now your user will choose anyone for example Addition. Now you further asked them to input two numbers and you will produce the result in front of them. Now what? if they want to use your calculator once more time what they will do? Do they run the program one more time? Will that be a good idea? A user runs a program again and again to use the calculator. Why don’t you allow them to use the calculator as much they want? Your program should exit when they wish to. So here you can sense that we need something which will iterate again and again but will exist only in certain conditions.
Now if you look at the program you will get the same as what I have explained above. We have used a while loop with the parameter true. Considering you have a clear understanding of the while loop, what it does is allows the program to execute the same set of code again and again.
Inside the while loop, we first ask the user what is their choice and we kept that choice in a python variable called choice. Now what we will do is to check which option they have sent us and based on that we will operate the same. For that, we first check in a condition. Since there are 4 options we have used the in function if the user input lies in 1,2,3 or 4 it will allow us further. For example user have input 3 then it will check if 3 is in (‘1’,’2',’3',’4'). We can see 3 is there so it will pass the condition. Inside that block, it will now ask for two numbers. So we have now simply used the input function to get the numbers one by one and store them in num1 & num2 variables and finally called our calculator function with those parameters, num1, num2, and the choice of the user. We will discuss the calculator function but let’s see what happened if the given number does not lie between 1–4. Here you can see we have a write the else part, we simply show it is an invalid number and we break the loop. You guys are well aware the break is the statement we have to use inside a loop to break the iteration. As I mentioned that we have to allow the user to run the program as many times as they want and only exist if they wish to. So in our program, it will run until it will get the choice between 1–4 once it will get anything out of this it will show invalid input and exit from the program. Hope now it is clear to you.
Now the final part, the calculator function.
def calculator(x, y, choice):
x = int(x)
y = int(y)
if choice == '1':
print(num1, "+", num2, "=", x + y)
elif choice == '2':
print(num1, "-", num2, "=", x - y)
elif choice == '3':
print(num1, "X", num2, "=", x * y)
elif choice == '4':
print(num1, "/", num2, "=", x / y)
In this function, we first convert the input in int(). The reason is we are using python 3.6 and assuming you guys are also using the same so what it actually does is consider any user input as a string even if it is a number. To determine the same you can use type() it will give the data type of the value.
So our first work is to convert the input in int because we have to do some mathematical operations. After that, we have to just simply decide what the user has given to us as a choice, and based on that we have just set the operator that’s it. Cool! right?
If the choice is 1 that means it will go in addition so we have to choose + operator, similarly for choice 2 we have to use operator -, for choice 3 we have to choose operator * and for choice 4 we have to choose operator /.
And the display the output you have just print the numbers and the result. Finally, it will look like below:
$ python3 calculator.py
Enter Your Choice(1 OR 2 OR 3 OR 4): 1
Enter first number: 3
Enter second number: 5
3 + 5 = 8
Enter Your Choice(1 OR 2 OR 3 OR 4): 2
Enter first number: 5
Enter second number: 2
5 - 2 = 3
Enter Your Choice(1 OR 2 OR 3 OR 4): 5
So you can see how we together write a mini calculator program in python. I believe it will help you a lot and grow your interest in python. Python is wonderful programming language. Let’s learn it together. Please write us to email@example.com. Also follow us on other social handles.
Thank you very much!!!
Originally published at https://www.codinghub.net.