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?