简体   繁体   中英

How to pass a variable from a function into the main code in Python

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?

You already pass these values to the function, ie 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)

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM