This Tutorial is the basics of coding with python which is a very useful programming language all over the world.
In this post i will show you how the algorithm and the coding process of a simple calculator works.
If you are new to this programming language it is is possible to download Python using this link Donwload Python
The calculator created here is for newbies in python that wishes to understand how it works and what could be done with it.
Here comes the coding part :
while True :
print("Options :")
print("Enter 'add' to add two numbers")
print("Enter 'subtract' to subtract two numbers")
print("Enter 'multiply' to multiply two numbers")
print("Enter 'divide' to divide two numbers")
print("Enter 'quit' to end the program")
user_input=input(" : ")
if user_input == 'quit' :
break
elif user_input == 'add' :
num1=float(input("Enter a number : "))
num2=float(input("Enter another number : "))
result=str(num1+num2)
print("The answer is : " + result )
elif user_input == 'subtract' :
num1=float(input("Enter a number : "))
num2=float(input("Enter another number : "))
result=str(num1-num2)
print("The answer is : " + result)
elif user_number == 'multiply' :
num1=float(input("Enter a number : "))
num2=float(input("Enter another number : "))
result=str(num1*num2)
print("The answer is : " + result)
elif user_number == 'divide' :
num1=float(input("Enter a number : "))
num2=float(input("Enter another number : "))
result=str(num1/num2)
print("The answer is : " + result)
This is the code that allows us to create a basic calculator using python ,in order to write down the code in the Py shell application after you open it , you should press on File on the corner and make a new folder where you can write down your code , it should look like this ;
In order to compile your code and make it work press run --> run module save your file and start calculating numbers to test the program.
A new Py shell module will run that will look like that :
The Meaning Of The Code
- while True : means loop forever , this statement will make the program work anytime that causes an infinite loop
- Print : this statement let us choose what we want to print it is the only thing that can be seen by peoples while the program is running
- user_input=input(" : ") : That is the input that the user of the program have to enter in order to make the program knows what we want , and the rest of the code is based on what the user choose and write as an input
- if user_input == 'quit' : break : that means if the user write "quit" while operating the program it will automatically break and close the pogram
- elif : means else if , it is often used when you want to make many if statement like the previous one
- For the second statement we take the example of adding so we put a name for num1 and num2 in order to continue the process , we make it equal to float
- float : which will make us able to calculate with decimals not only integers if the input was 2+3 the calculator will give 5.0 not 5 because it works with decimals hence it is possible to add 2.5 + 3.2 = 5.7
- (input("Enter a number : ")) : again the input statement enable us to write down the number we want to add or subtract ...
- result=str(num1+num2) : that is the value we associated to the result which the the num1 (1st number we added) + num2 (the second number we want to add)
- print("The answer is : " + result ) : in that final step we printed a sentence that indicated what is the answer and + result which is calculated as num1+num2 here it give us the result
- str : the str statement in the result make us able to add the result to a sentence which is string
The Final Result Would Look Like This :
Comment Below For Suggestions !
opps ...Great post about Python... Thanks universe.laws for sharing this post..Because its very informative post for me...So thank you so much
python will always be my favorite.