Click here to Skip to main content
15,748,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 pay 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.

What I have tried:

def computepay(h, r):
    return 498.75

hrs = input("Enter Hours:")
p = computepay(45, 10.5)
print("Pay", p)
Updated 9-Aug-22 17:01pm
Dave Kreskowiak 8-Dec-21 11:57am    
...and your question would be ...?
CHill60 8-Dec-21 12:01pm    
Nope - that code runs fine

1 solution

There is no error with that code as such, I suspect the "error" is a comment from your tutor

You have hard-coded the answer in your computepay function but your tutor wants you to calculate it from the input i.e.
def computepay(h, r):
    return float(h) * float(r)
I put the float() function in because Fiddle moaned at me and I couldn't be bothered to work out why if I'm being honest

You are also hard-coding the values to calculate instead of prompting for them. This line needs to change
p = computepay(45, 10.5)
You also need an additional input() call for the Rate.

I will leave these last two points to you as this is your homework
Share this answer
Richard MacCutchan 8-Dec-21 12:23pm    
Python is quite happy with return h * r.

Quote from section 3.1.1 of The Python Tutorial — Python 3.9.9 documentation[^]: operators with mixed type operands convert the integer operand to floating point.
CHill60 8-Dec-21 12:28pm    
I thought that was the case! I was using the fiddle at and it really didn't like that. Possibly because of the way the site captures "input" - as strings effectively.
Richard MacCutchan 8-Dec-21 12:33pm    
Yes, my own experience of online compilers is that they do not always match the "real" ones. So the results can be confusing, or in some cases, actually incorrect. And running Python locally is so easy ...
CHill60 8-Dec-21 13:29pm    
Not on my work machine :-( Red tape gone mad here
Richard MacCutchan 8-Dec-21 13:56pm    
I haven't 'worked' since mid 2006. And as a colleague of mine once said, "'work' is much too large a word for what I do". :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900