Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Can someone take a look at my code, please !!!

Hi there ! I was challenging myself with code exercise (found online ) and i stuck, can't figure out what is wrong with my code. So here is the challenge question :

"Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of time-and-a-half in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function."

And this is my code : ! Imgur

2 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

That's a fun exercise - what site is that from?

Your code is failing because you are messing up your variables. You're not passing the correct values as arguments on the call and your are not using the correct parameter values in computpay() function.

Also, the challenge is asking you to only compute the time in a half in the computpay() function, not all the pay.

Here's how I would approach it:

def computepay(hours, rate):
    return hours * (rate * 1.5)


hours_in = float(input("Enter Hours: "))
rate_in = float(input("Enter Rate: "))

regular_hours = hours_in if hours_in <= 40.0 else 40.0
ot_hours = 0 if hours_in <= 40.0 else hours_in - 40.0

pay = (regular_hours * rate_in) + computepay(ot_hours, rate_in)

print("Total Pay is ---> " + str(pay))

:tropical_drink: :palm_tree:

thank you, Dave !