Basics of Python - 2 Digit Calculator

by tanish satpal in Teachers > 7

83 Views, 1 Favorites, 0 Comments

Basics of Python - 2 Digit Calculator

Image 27-08-24 at 11.14 AM.jpeg

Welcome to the Basics of Python. We will start of with a basic 2 digit calculator. This will use variables, and introduce you to the intricacies of python

Supplies

You will need a program that processes Python. I use Google Colab, but you can use Pycharm or any other application

Making the Variables

Image 27-08-24 at 11.26 AM.jpeg

So, we start of with variables. They let us store data. But, we have to define variables first. So, let the variables be numero1 and numero2. Now, we need it to be inputted from the user, so we can use it for our calculations.

So the code till now is:

print("Welcome to variable work")

numero1 = int(input("Number 1?"))

numero2 = int(input("Number 2?"))

--------

Now, you might notice the "INT" symbol before the input. It converts the input from a string(Text), to an integer(usable numbers). The limitation of it, however, is that it can't be used with decimal calculations. However, there is a simple fix to it. Instead of using "INT", we can use "FLOAT", which gives us the access to use decimals. So now the code will be:

print("Welcome to variable work")

numero1 = float(input("Number 1?"))

numero2 = float(input("Number 2?"))

Choose the Type of Calculation

Image 27-08-24 at 11.36 AM.jpeg

Now, we give the user a choice for its calculation. For this we will need a variable, as it is the only way data is stored in Python. So, Our code would look like this:

Way = input("Choose your operation: Addition, Subtraction,Multiplication,Division.")

--------

But, the issue appears now. For addition, they could write "+, Add, Addition, additon." This means that we have to make sure the user is limited to only 1 method of conveying. So, for Add, the user will write "A", Subtract will be "S", and so on. We must make sure that we write in a specific case, so that it reduces our work. So our code will look like this:

Way = input("Choose your operation: (A)ddition, (S)ubtraction,(M)ultiplication,(D)ivision. Write in uppercase only.")

-----

This fixes the issue. Now the user knows he can only write A,S,M,D.

If Statements

Image 27-08-24 at 11.50 AM.jpeg

So, we have given the user a choice of A,S,M,D. So, we will have to write code based on it. Now, we are introduced to the "IF" command. It gives us options. The way to do it is simple, much easier than other coding languages. It works like this.

if [variable] [Symbol of logic] [A number/string]:

#The task is done

elif [variable] [Symbol of logic] [A different number/string]:

#Different task is done

else:

#Another task is done

------

ELIF gives you more options, while ELSE suggest that it works if anything else is left/ possible in the options. So, our code will have:

if Way=="A":

#add

elif Way=="S":

#subtract

elif Way=="M":

#Multiply

elif Way=="D":

#Divide

else:

#error message

------

If you might have noticed, there are 2 equal to signs. Well, in python, "equal to" looks like,"==". This a limtation we have to cooperate with

Doing the Calculation

Image 27-08-24 at 12.01 PM.jpeg

Calculations are very easy. They look like this:

Answer = (num1 [Symbol] num2)

-----

So for addition, it would look like this:

answer = (numero1 + numero2)

-----

The symbols are +,-,*,/

Just replace the symbols the symbols to get the answer.

Now, we need to print it to the user. So we use the print command. Commas lets us connect strings and integers into 1 sentence. It looks like this:

print ("String1",number,"String2")

-----

It can continue as long as you want. So for addition, it would look like this:

answer = (numero1 + numero2)

print(numero1,"+",numero2,"is:", answer)

----

This will show the user it clearly. Copy it for the rest of the code and it will look like this:

if Way== "A":

answer = (numero1 + numero2)

print(numero1,"+",numero2,"is:", answer)

elif Way== "S":

answer = (numero1 - numero2)

print(numero1,"-",numero2,"is:", answer)

elif Way== "M":

answer = (numero1 * numero2)

print(numero1,"*",numero2,"is:", answer)

elif Way== "D":

answer = (numero1 / numero2)

print(numero1,"/",numero2,"is:", answer)

else:

print("Oh no! Something went wrong. Please try again.")

Division Issues

Image 27-08-24 at 12.02 PM.jpeg
Image 27-08-24 at 12.08 PM.jpeg

Try and divide by 0. Its impossible. So, the computer will give back an error. Check out the image above. We need to prevent it from happening. So, we must make sure the use doesn't divide by 0. There is a simple fix to it, using IF statements. How, well:

If the 2nd number is = 0, then:

print "Can't divide by 0."

else:

Do the division

-----

In python, it would look like:

if numero2 == 0:

print("Can't divide by 0. Try again.")

else:

answer = (numero1 / numero2)

print(numero1,"/",numero2,"is:", answer) .


This removes the error, as the user is stopped from dividing by 0.

The End

Image 27-08-24 at 12.11 PM.jpeg

And thats it! The code allows us to use 2 digits for addition, subtraction, multiplication and division. You can continue for more complex calculation like factorials, etc.

I am enclosing the google colab notebook for python basics for you to view. Bye now!

https://colab.research.google.com/drive/1JRapgvN5cfsyea6Zu8M3TeRaNvxIVjO5?usp=sharing