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

TRY IT YOURSELF

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).

`try:  widget_sold = float(input("Please input the number of widgets sold "))  widget_price = float(input("Please input the average price per widget "))except:  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.8else:  post_tax = pre_tax*0.85print("Pre-Tax Earnings: ", pre_tax )print("Post-Tax Earnings: ", post_tax) `

--

--

--

More from Foundational Data Science: Interview Questions

Data Science tutorial working through solutions to Data Science Interview Questions

Abish Pius

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