0

Hi i am new to programming and just started learning python i wrote the below code /program to prompt hours and rate per hour using raw_input to compute gross pay. i tried to initiate time-and-a-half for the hourly rate for all hours worked above 40 hours. 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. i used 45 hours and a rate of 10.50 per hour to test the program (the pay should return 498.75). i tried using raw_input to read a string and float() to convert the string to a number. i Don't name my variable sum or use the sum() function. i am able to print the output but i need to input the data when prompted unlike inputing the value in "line16"

def computePay (hours,rate):
    if hours > 40:
        overtimerate = 1.5 * rate
        overtime = (hours-40) * overtimerate
        overtimepay = overtime + (40 * rate)
        return overtimepay;
    else:
        normalpay = hours * rate
        return normalpay;
hours = raw_input('Enter hours: ')
hrs = int(hours)
rate = raw_input('Enter rate: ')
r = float(rate)
p = computePay(45,10.50)
print p
6
  • 1
    What's your question, exactly? Why do you take input then ignore it? Also the indentation in your code sample is all over the place, please fix it and provide a minimal reproducible example. Commented Jan 11, 2017 at 0:16
  • Just use the variables that hold the data you want: p = computePay(hrs,r). Commented Jan 11, 2017 at 0:19
  • tdelaney, thanks a lot for taking time and reviewing my request. i tried P = computePay(hrs, r) but getting an error "You must use a function called computepay to do the computation." Commented Jan 11, 2017 at 2:08
  • You're told you're asked for a computepay function, but your function is called computePay... Is this a typo? Commented Jan 11, 2017 at 4:23
  • Where is the raw_input coming from? Is this submitted online? Make sure everything is spelled how they want it Commented Jan 11, 2017 at 4:23

2 Answers 2

1

Assumptions:

I had to make the following assumptions in understanding your question:

  1. Looking at your print statement, You are using python2.x
  2. You want to computePay using the user inputted hours and rate.

Problem:

In the following line you are using constant hrs and r values instead of using the user inputed values

p = computePay(45,10.50)

Solution:

If you want to use user inputted values to compute the pay, you need to call the function as follows:

p = computePay(hrs, r)

With this line you are essentially asking python to computePay using the values stored in the variables hrs and r.

Final Code:

Therefore your final code should like this:

def computePay (hours,rate):
    if hours > 40:
        overtimerate = 1.5 * rate
        overtime = (hours-40) * overtimerate
        overtimepay = overtime + (40 * rate)
        return overtimepay;
    else:
        normalpay = hours * rate
        return normalpay;
hours = raw_input('Enter hours: ')
hrs = int(hours)
rate = raw_input('Enter rate: ')
r = float(rate)
p = computePay(hrs,r)
print p

Sample Output:

Enter hours: 55
Enter rate: 20
1250.0
Sign up to request clarification or add additional context in comments.

6 Comments

thanks a lot for taking time and reviewing my request. i tried P = coumputePay(hrs, r) but getting an error "You must use a function called computepay to do the computation."
@vgrg Can you please post the code you are using now? and the actual error python is giving out. Couple of sanity checks: 1. You need to define computePay function before calling it. 2. You can try copy pasting the "final code" from my solution. It should work.
code below..1) yes! you are correct,i am trying to define computePay function before calling it. 2) i tried copying your code but its the same error #You must use a function called computepay to do the computation.#
def computePay (hours,rate): if hours > 40: overtimerate = 1.5 * rate overtime = (hours-40) * overtimerate overtimepay = overtime + (40 * rate) return overtimepay; else: normalpay = hours * rate return normalpay; hours = raw_input('Enter hours: ') hrs = int(hours) rate = raw_input('Enter rate: ') r = float(rate) p = computePay(hrs,r) print p
@vgrg As others have pointed out, Python is case sensitive. So there is computepay is a different fucntion from computePay. So I suggest you upload a screenshot of your code and your error as an edit in your question. Then we might be able to better understand the issue.
|
0

If you mean by that you want the arguments to equal the inputs rather than the ones inputted manually, you should do instead:

p = computePay(hrs, r)

Example output:

Enter hours: 45
Enter rate: 10.5
498.75

1 Comment

Anthony, thanks a lot for taking time and reviewing my request. i tried P = computePay(hrs, r) but getting an error "You must use a function called computepay to do the computation."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.