0

I've been given a homework assignment to write a Python program to compute a worker’s pay, based on a rate per hour and the number of hours worked. So far I've come up with the following code...

#Function
def calculatePay(rateHour,nHours):
    if nHours <= 40:
        pay = rateHour * nHours
    elif nHours < 60:
        pay = (rateHour * 40) + ((nHours - 40) * (rateHour * 1.5))
    else:
        pay = (rateHour * 40) + (20 * (rateHour * 1.5)) + ((nHours - 60) * (rateHour * 2.0))
    return pay

#Main Code
pay1 = calculatePay(30, 20)
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay1)
print()
pay2 = calculatePay(15.50, 50)
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay2)
print()
pay3 = calculatePay(11, 70.25)
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay3)
print()

rateHour = int(input('Enter the rate per hour: '))
nHours = int(input('Enter the number of hours worked: '))

pay4 = calculatePay(rateHour,nHours)
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay4)
print()

When I run it I get the following error...

Traceback (most recent call last):
  File "C:\Users\John\Desktop\Python Programming\JohnLissandrello_Homework3.py", line 15, in <module>
    print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay1)
NameError: name 'nHours' is not defined

I believe it's because I'm trying to use local variables rateHour and nHours in my main code.

How do I pass those two variables from my function into the main code so I can output the rateHour and nHours along with the calculated pay?

0

2 Answers 2

3

You already pass these values to the function, i.e. you have them outside the function. Problem is: they don't have a name.

Suggestion:

#Main Code
ratehour = 30
nHours = 20
pay1 = calculatePay(ratehour, nHours)
print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay1)
print()

If you look at your code, you'll find a lot of duplicate lines. You can put that into a method again:

def printPay(ratehour, nHours):
    pay = calculatePay(ratehour,nHours)
    print('You worked ', nHours, 'hours at a rate of ', ratehour, 'per hour, you will be paid $ ', pay)
    print()

and then do it in a loop:

for rate, hours in  [(30,20), (15.5,50), (11,70.25)]:
    printPay(rate, hours)
Sign up to request clarification or add additional context in comments.

Comments

1

@Thomas has nailed the answer but there are a couple more issues:

Lines 21 & 22 you have called int but lines 14 and 17 makes it look like you want to use floats.

Line 25 you have ratehour but 21 & 24 have rateHour - Python is very picky about capitalisation

Comments

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.