Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculation of wage based on inputs in python

I have a python problem and I can’t seem to get to the finish line with this question for homework. Basically, the problem is:

  • an employee’s wage is $36.25p/h. a normal working week is 37 hours.
  • if he works more than 37 hours he gets paid 1.5 times the normal rate ($54.375)
  • if the employee sells 6 cars or more a week he receives a bonus of $200 per car sold.

TASK: Write a program that takes as input the number of hours worked and the total number of cars sold for the week, and outputs the car dealer’s total salary for the week.

Here is the code I have so far:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

hours = int(input("How many hours were worked?"))
salary = float(input("The salary"))
cars = int(input("Total number of cars sold for the week?"))
total = hours * float(salary)
    
if hours > 37 and cars > 5:
    print("The salary is:",int(cars) * int(200) / 10 + 1.5 * total )
elif hours <= 37:
    print("The salary is:",total)

>Solution :

try this:

BASE_HOURS = 37
WAGE_MULTIPLIER = 1.5
CAR_SALES_BONUS = 200
SALES_NEEDED_FOR_BONUS = 6

hours = int(input("How many hours were worked? > "))
wage = float(input("The wage > ")) 
cars = int(input("Total number of cars sold for the week? > "))

if hours > 37:
    total = BASE_HOURS * wage + (hours - BASE_HOURS) * (wage * WAGE_MULTIPLIER)

else:
    total = hours * wage

if cars >= SALES_NEEDED_FOR_BONUS:
    total += CAR_SALES_BONUS

# ...

As you can see I took my freedom to refactor the code quite a bit to improve structure & reduce code smell. Furthermore some variable names like salary where quite unfitting if I’ve interpreted the sense behind the script correctly, as we’re interested in the hourly wage of the person, not his salary.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading