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