FlexBoxer
FlexBoxer

Reputation: 131

How do I fix this hourly rate calculator?

Keep getting bad input errors on this python code. Can someone walk me through what I'm doing wrong? Thanks. The task is that the code works out time-and-a-half for the hourly rate for all hours worked above 40 hours. Using 45 hours and a rate of 10.50 per hour to test the program, the pay should then be 498.75. I keep getting 708.75...

hrs = input("Enter Hours:")
h = float(hrs)
rate = input("Enter Rate:")
r = float(rate)
double_r = r * 1.5

total = 0.0

if h <= 40.00:
    total = h * r  

elif h > 40.00: 
    total = h * double_r

print(total)

Upvotes: 1

Views: 470

Answers (3)

Benedikt
Benedikt

Reputation: 126

Your problem isn't a coding problem but a math problem: You're multiplying every hour with the double_r rate (45 * 10.5 * 1.5 = 708.75). If you only want the hours above 40 hours to be multiplied with the higher rate then you have to multiply them extra (40 * r for the normal rate and (h-40) * double_r for the rest with the better rate. Your code should look like this:

if h <= 40.00:
    total =  h * r  

elif h > 40.00: 
    total = 40 * r + (h - 40) * double_r

Upvotes: 1

Riven
Riven

Reputation: 375

hrs = float(input("Enter Hours: "))
rate = float(input("Enter Rate: "))

double_rate = rate * 1.5

total = 0.0

if hrs <= 40.00:
    total = hrs * rate  

elif hrs > 40.00: 
    total = ((hrs - 40 ) * double_rate) + (40 * rate)

print(total)

Upvotes: 2

Inder Preet
Inder Preet

Reputation: 114

Is this bad input error or logical error ? I don't have solution for first one, but I surely have it for the second part.

According to your code, If hours are <=40 , you are multiplying the hour with the rate. but if it greater than 40, you are multiply the hour with the rate with 1.5 . Here the logic is going wrong. You just need to add the extra 1.5 for those hours which are greater than 40.

For that, you would have to modify your total statement. Something like this : total = ((h - 40 ) * double_r) + (40 * r)

So for 45 hours with 10.5 rate , it would be 40 * 10.5 = 420 and 510.51.5 = 78.75 thus resulting in 498.75

If this helps, please upvote. :)

Upvotes: 1

Related Questions