3 Replies - 220 Views - Last Post: 14 February 2019 - 04:50 AM Rate Topic: -----

#1 fmak916   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-June 18

Python Payroll Help

Posted 06 February 2019 - 01:10 AM

Hello
Just started a python class a week ago. Working on this simple payroll program. I'm getting stuck on 'NameError: name 'rate' is not defined'
spent an hour trying to figure out whats wrong but couldn't. Any advice would be appreciated. Thanks!
here my code:
#Calculate Pay

def calculatePay(rate, hours):
    if hours <= 40:
        grossPay = (rate * hours)
        
    elif hours > 40 and hours <= 60:
        overtime = hours - 40
        normalHours = 40
        grossPay = (rate * 40) + (hours - 40) * (rate * 1.5)
    else :
        grossPay = (rate * 40) + (20 * 1.5 * rate) + (hours - 60) * (2 * rate)

    return grossPay
    
        

#Main Code


pay1 = calculatePay(30, 20)
pay2 = calculatePay(15.50, 50)
pay3 = calculatePay(11, 70.25)

print ('Your grosspay is: $', pay1)
print ('Your grosspay is: $', pay2)
print ('Your grosspay is: $', pay3)

userPay = input('Please enter your pay rate: ')
userPay = float(userPay)

userHours =input('Please enter the amount of hours: ')
userHours =float(userHours)

totalPay = calculatePay(rate, hours)
print ('Your total pay is: ', totalPay)


'

Is This A Good Question/Topic? 0
  • +

Replies To: Python Payroll Help

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7420
  • View blog
  • Posts: 15,382
  • Joined: 16-October 07

Re: Python Payroll Help

Posted 06 February 2019 - 01:36 AM

You'll also be getting an error on hours...

In totalPay = calculatePay(rate, hours) you haven't defined the variables you're passing. You have asked the user for rate and hours, but used different names, which is fine. Use those names.
Was This Post Helpful? 1
  • +
  • -

#3 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 513
  • View blog
  • Posts: 1,633
  • Joined: 27-December 13

Re: Python Payroll Help

Posted 06 February 2019 - 02:13 AM

Just to supplement the answer by baavgai:

The function arguments does not have to have the same name in the call as inside the function; look at this:
def function(argument1, argument2):
    # some code here

input1 =  # some value
input2 =  # some value 
result = function(input1, input2)


In this small code, the calling arguments are named input1 and input2; whereas inside the function they are named argument1 and argument2.
This is perfectly ok, but sometime confuses beginners.
Was This Post Helpful? 1
  • +
  • -

#4 fmak916   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-June 18

Re: Python Payroll Help

Posted 14 February 2019 - 04:50 AM

Sorry for the late reply. Thanks a bunch really helped.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1