Build a Net Income Calculator App with Python

python logo
Standard

Share this tutorial w/ your followers on Twitter. Click this link to tweet this tutorial: https://goo.gl/o7nTMZ

This tutorial is for beginners who want to learn how to build applications in Python programming language.

Important Note

If you’re using Python 3, you should use:

  • input() instead of raw_input()
  • print() instead of print. Example: instead of print “hello”, you should use print(“hello”)

What will we build?

We will build a net income calculator for freelancers in an imaginary company that serves as a middleman between a freelancer and an employer. To be able to focus on Python, we won’t build a web based application so you won’t have to use HTML. What we will build is a very simple command line application.

Requirements

  • The user should be able to input his/her hourly rate.
  • The user should also be able to input the number of hours he/she worked.
  • We will assume that our imaginary company deducts $1 + 10% of the gross income of the user .
  • The gross income, net income and deductions should be shown to the user.

Building the command line application

Create a file and name it income_calculator.py

Before starting to write code, it’s a good practice to plan how you will build the application first. Let’s do that by adding comments inincome_calculator.py

# ask the hourly rate of the user
# convert hourly rate to float
# ask the number of hours that the user has worked
# convert hours worked to float
# multiply the hourly rate and the number of hours worked
# get the 10% of the gross income
# add 1 to the deduction
# subtract the deductions to the gross income
# show the gross income, net income and deductions to the user

Now that we have a clear idea on what are the steps that we should code to achieve the expected result, let’s start writing code.

# ask the hourly rate of the user
hourly_rate = raw_input(“What is your hourly rate?”)

raw_input() is a built-in function in Python that we can use to gather inputs from a user. What this line of code basically does is ask the hourly rate of the user and stores the input of the user in the hourly_rate variable.

# convert hourly rate to float
hourly_rate = float(hourly_rate)

The result of raw_input() is a string so we are converting it to a float for us to be able to use it for math operations later.

# ask the number of hours that the user has worked
hours_worked = raw_input(“How many hours did you worked?”)
# convert hours worked to float
hours_worked = float(hours_worked)

What the code above does is similar to the code that I’ve discussed earlier. It just asks the number of hours that the user worked, store it in thehours_worked variable then converts it to a float.

# multiply the hourly rate and the number of hours worked
gross_income = hourly_rate * hours_worked

To get the gross income of the user, we are multiplying the user’s hourly rate and the number of hours worked. We are storing the result in thegross_income variable.

# get the 10% of the gross income
deduction = gross_income * .10

We are multiplying gross income and .10 to get the 10% of gross income. We are storing the result in the deduction variable.

# add 1 to the deduction
total_deductions = deduction + 1

Since the company also deducts $1 to the gross income of the user, we are adding 1 to the value of deduction variable and we are storing the result tototal_deductions variable.

# subtract the deductions to the gross income
net_income = gross_income — total_deductions

To compute the net income of the user, we are subtracting the total deductions to the gross income.

# show the gross income, net income and deductions to the user
print “Gross income: “ + str(gross_income)
print “Net income: “ + str(net_income)
print “Total deductions: “ + str(total_deductions)

This block of code exactly does what the comment says. It will show the gross income, net income and deductions to the user. In Python, since we can’t concatenate a string (Example: “Gross income”) and a float (gross_income), we have to convert gross_income to a string. We can convert a float to a string by using Python’s built-in function str()

The contents of income_calculator.py should now look like this:

# ask the hourly rate of the user
hourly_rate = raw_input(“What is your hourly rate?”)
# convert hourly rate to float
hourly_rate = float(hourly_rate)
# ask the number of hours that the user has worked
hours_worked = raw_input(“How many hours did you worked?”)
# convert hours worked to float
hours_worked = float(hours_worked)
# multiply the hourly rate and the number of hours worked
gross_income = hourly_rate * hours_worked
# get the 10% of the gross income
deduction = gross_income * .10
# add 1 to the deduction
total_deductions = deduction + 1
# subtract the deductions to the gross income
net_income = gross_income — total_deductions
# show the gross income, net income and deductions to the user
print “Gross income: “ + str(gross_income)
print “Net income: “ + str(net_income)
print “Total deductions: “ + str(total_deductions)

Now let’s run our command line application by running this command in your terminal:

python income_calculator.py

It should ask your hourly rate and number of hours worked then show your gross income, net income and total deductions.

python income calculator

Feel free to leave a feedback about the tutorial by leaving a comment. I would really appreciate it if you will recommend and also tweet this tutorial 🙂

Share this tutorial w/ your followers on Twitter. Click this link to tweet this tutorial: https://goo.gl/o7nTMZ

Want to get updated when I publish another tutorial? Subscribe:http://eepurl.com/bVGMwr

Gain the skills you need to be a pro at Python development. Start learning for FREE: http://treehouse.7eer.net/c/245500/245646/3944

Are you looking for a remote/home based job? Visithttp://digitalnomadgigs.com/

You can follow me on Twitter: https://twitter.com/johnkevinmbasco

Looking for a developer? I’m accepting web/mobile development projects. Shoot me an email: [email protected]