Q#25: Quarterly earnings calculator

Suppose a company is taxed 20% if earnings for a given quarter are > = $3M USD.
If earnings land < $3M USD for the quarter, the company is taxed at a lower rate of 15%.
Write a function using Python to calculate both the pre-tax and post-tax earnings for a given company, with the ability to feed in the # of widgets sold as well as the average price of the widget as inputs (# of widgets sold * avg. price of widgets = total earnings in this case).
For example, if the company sells 20,000 widgets at an average price of $220 USD then your function should return:

  • Pre-tax earnings were 20,000*220 = 4.4M for the quarter.
  • Post-tax earnings were 3.52M for the quarter [4.4M *.8 (since we fall in higher 20 percent tax bracket here)]
  • Credit to: team@interviewqs.com



This question tests your grasp of the Python programming language and is rather simple and straightforward.

The first step is to take in user inputs (both widgets sold and average price of widget), and in python we can use the function input(). However, this function encodes the user input as a string. We need to wrap this inside a float() call to convert their input into a number. Finally, as good practice lets raise an exception in case the user tries some funny business (this is optional/extra as the problem did not call for it).

widget_sold = float(input("Please input the number of widgets sold "))
widget_price = float(input("Please input the average price per widget "))
print("Please make sure the inputs are numbers.")

The final step is to write our outputs using the variables we have and the formulas provided by the question. First, lets start by calculating the total earnings (pre-tax earnings), then use if-else statements to determine our tax bracket.

pre_tax = widget_sold*widget_priceif pre_tax >= 3000000:
post_tax = pre_tax*0.8
post_tax = pre_tax*0.85
print("Pre-Tax Earnings: ", pre_tax )
print("Post-Tax Earnings: ", post_tax)




Data Science tutorial working through solutions to Data Science Interview Questions

Recommended from Medium

How to start learning the front-end development

Modern way of requesting Permission in Android

How to Organise a Large Program in Python (Part-2).

Takashi Ronin

How to Choose the Best Magento Development Agency?

WeCloud Chat As A Service

Asp.Net Core 5 ile CQRS ve MediatR Kullanılması

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abish Pius

Abish Pius

Data Science Professional who teaches with unflashy, simple to understand python code.

More from Medium

Let’s Create a Summarizer Extension for Browsers — Part 1

width of widgets in tkinter

tkinter code creating an Entry and a Button and setting the width of each to 35

How to create a Telegram bot with Python in under 10 min!

Create executable file on window from your python code using Power shell.