Subtraction Program in Python
In this Python programming, tutorial we are going to create a simple Subtraction Program in Python. In this program, we are going to add the ability to use floats not integers.
Functions and Methods Used in This Program
- input()
- float()
- print()
- .format()
- round()
Step 1 – Open Your Text Editor
Open your text editor in this tutorial we use Sublime Text 3
Step 2 – Save your file
Save your file by pressing command + s or control + s. Name your file subtraction.py and save to your desktop.
Step 3 – Create Welcome Print Statement
We want to welcome our users to our program so we will add a simple print statement doing so.
#Create Welcome Print Statement
print("Welcome to our subtraction program")
Step 4 - Get the users first number
In this step, we will get the users number using input() and then convert that number to a floating point number using float(). Then we will assign the value to a variable called firstnum.
#Get the users first number
print("Welcome to our subtraction program")
firstnum = float(input("What is your first number. "))
Step 5 – Get the users second number
This step is similar to step 4 with the only difference being we are going to assign the value to the user gives to a new variable called secondnum.
#Get the users second number
print("Welcome to our subtraction program")
firstnum = float(input("What is your first number. "))
secondnum = float(input("What is your second number. "))
Step 6 – Get Users Difference
This step we will get the users difference between two numbers and round the number to two numbers after the decimal and save the difference to a variable called thesum.
#Get Users Difference
print("Welcome to our subtraction program")
firstnum = float(input("What is your first number. "))
secondnum = float(input("What is your second number. "))
thesum = round(firstnum - secondnum, 2)
Step 7 – Print The Difference Back to User
This is our final line of code in our little program here. We want to print the difference back to the user so to do that we use a print() statement and format the string print backs the value within thesum.
#Print The Difference Back to User
print("Welcome to our subtraction program")
firstnum = float(input("What is your first number. "))
secondnum = float(input("What is your second number. "))
thesum = round(firstnum - secondnum, 2)
print("Your difference is ".format(thesum))
Step 8 – Run The Program
Open your terminal or command prompt. Change into your desktop and run the program by typing python3 subtraction.py
#Run The Program
Thomass-MBP:~ Tommy$ cd desktop
Thomass-MBP:desktop Tommy$ python3 subtraction.py
Welcome to our subtraction program
What is your first number. 4
What is your second number. 5
Your difference is -1.0
Now that you have seen the way to build a simple subtraction program in Python with floating point numbers. Play with the program try to improve it and make your own modifications. If you can break let us know so we can do a tutorial how to improve the subtraction program. If you have any questions leave a comment below and we will help you.
Subtraction Program In Python - Python Programs
No comments:
Post a Comment