I have to create a python program for class-
the instructions are-
Write a payroll program that pays time and a half for anything over 40 hours. This should have 3 functions in #addition to main.
Which I did without the 3 funcitons.

def main():   
  hours = float(input('How many hours did you work: '))
  while hours < 8 or hours > 86:
      print ("Error- Hours must be at least 8 and less than 86")
      hours = float(input('re-enter hours worked: '))        

  rate  = float(input('What is your hourly rate? '))
  while rate < 7 or rate > 50:
      print ("Error- Payrate must be at least $7.00 and less than $50.00")
      rate = float(input('re-enter your hourly rate: '))

  if hours <= 40:
      print ("           Payroll Information")
      print ("Pay rate          $",format(rate,'7.2f'))
      print ("Regular Hours        ", format (hours, '2.0f'))
      print ("Overtime Hours         0")
      print ("Regular pay       $", format (hours*rate, '7.2f'))
      print ("Overtime pay      $    0.00")
      print ("Total pay:        $", format (hours*rate, '7.2f'))
  else:
      othours = hours - 40


    otpay = othours * (rate * 1.5)
    regularpay = 40 * rate

    print ("     Payroll Information")
    print ("Pay rate          $", format (rate,'7.2f'))
    print ("Regular Hours        ", format (hours, '2.0f'))
    print ("Overtime Hours       ", format (othours, '2.0f'))
    print ("Regular pay       $", format (hours*rate, '7.2f'))
    print ("Overtime pay      $", format (otpay, '7.2f'))
    print ("Total pay:        $", format (regularpay+otpay,'7.2f'))

main()

Output

How many hours did you work: 50
What is your hourly rate? 8

Payroll Information


Pay rate          $    8.00
Regular Hours         50
Overtime Hours        10
Regular pay       $  400.00
Overtime pay      $  120.00
Total pay:        $  440.00

But now the teacher is hitting me with these instructions and I can't figure out how to seperate things to make him happy. When I start to seperate functions I get errors, or nothing prints.

  1. The first function asks the user how many total hours were worked and the pay rate and returns this information to main. These values must be validated. Hours worked must be at least 8 and no more than 86. Pay rate cannot be less than $7.00 or more than $50.00.
  2. The second function calculates the regular hours and overtime hours and returns this information to main. A person might work less than 40 hours so you must allow for that situation.
  3. The third function calculates the regular pay, (regular hours times pay rate); overtime pay, (overtime hours times overtime pay rate) and the total pay and returns this information to main.
  4. Main will then display this information on the screen like the sample below. (Values will have to be #passed and returned).

The theacher is right. Keep him/her happy :) If interested, I can elaborate on this further.

The main function should look like:

def main():
    hours,rate=get_user_input()
    regularhours,overtimehours=calculate_hours(hours)
    regularpay,overtimepay,totalpay=calculate_pay(regularhour,overtimehours,rate)
    #printing results

The calculate_hours functions is like:

def calculate_hours(hours):

    if hours<=40:
        return hours,0
    else:
        return 40,hours-40
def getInput():
    hours = float(input('How many hours did you work: '))
    while hours < 8 or hours > 86:
        print ('Error- Hours must be at least 8 and less than 86')
        hours = float(input('re-enter hours workde: '))

    rate = float(input('What is your hourly rate?: '))
    while rate < 7 or rate > 50:
        print ('Error- Payrate must be at least $7.00 and less that $50.00')
        rate = float(input('re-enter your hourly rate: '))

    return hours, rate

def timeCal(hours):
    if hours <= 40:
        return [hours, False]
    else:
        hour = hours - 40
        return [hour, True]

def rateCal(hours, rate):
    if hours[1]:
        othours = hours[0]
        otpay = othours * (rate * 1.5)
        regularpay = 40 * rate
        return otpay, regularpay, othours
    else:
        return None

def main():
    hours, rate = getInput()
    if rateCal(timeCal(hours), rate) == None:
        print('Payroll Information\n')
        print('Pay rate  $',format(rate,'7.2f'))
        print('Regular Hours  ',format(hours, '2.0f'))
        print('Overtime Hours  0')
        print('Regular pay  $',format(hours * rate,'7.2f'))
        print('Overtime pay  $ 0.00')
        print('Total pay:  $',format(hours * rate, '7.2f'))

    else:
        otpay, regularpay, othours = rateCal(timeCal(hours), rate)
        print('Payroll Information\n')
        print('Pay rate  $',format(rate,'7.2f'))
        print('Regular Hours  ',format(hours, '2.0f'))
        print('Overtime Hours  $',format(othours, '2.0f'))
        print('Regular pay  $',format(hours * rate,'7.2f'))
        print('Overtime pay  $',format(otpay, '7.2f'))
        print('Total pay:  $',format(regularpay + otpay, '7.2f'))


main()

Thank you very much, this is very helpful. I have the same assignment and this is what I have done:

name=input ("Please enter your name: ")
hours=int (input ("Please enter number of hours: "))
rate=int (input ("Please enter your rate per hour: "))

def payroll (hours, rate):
    if hours <=40:
        print ("Your regular pay amount is: ${}".format(hours*rate))
        print ("Your total payment is: ${y}".format(y=hours*rate))
        return hours,rate

    elif hours >41:
        print ("Your regular payment amount is: ${}".format(40*rate))
        print ("Your overtime payment amount is: ${}".format(((hours-40)*rate)*1.5))
        print ("Your total payment is: ${x}".format (x=(40*rate)+ ((hours-40)*rate)*1.5))
        return hours,rate

payroll(hours,rate)

-->However, my teacher also require the payment amount in number and words, for ex: 295 (two hundred and ninety five/100). I would be very appreciate if anybody can give me a hint.

Thank you.

but both of solution cannot print decimal number such as 1.45...?

This is what I've done, but I dont know why it couldnot run. Please advice. I just started Python class for couple week, I am really appreciate all of your reference but its over my understanding.

def Ones(n):

    if n==1:
        return "One"
    elif n==2:
        return "Two"
    elif n==3:
        return "Three"
    elif n==4:
        return "Four"
    elif n==5:
        return "Five"
    elif n==6:
        return "Six"
    elif n==7:
        return "Seven"
    elif n==8:
        return "Eight"
    elif n==9:
        return "Nine"

def Teens(n):
    if n==10:
        return "Ten"
    elif n==11:
        return "Eleven"
    elif n==12:
        return "Twelve"
    elif n==13:
        return "Thirteen"
    elif n==14:
        return "Fourteen"
    elif n==15:
        return "Fifthteen"
    elif n==16:
        return "Sixteen"
    elif n==17:
        return "Seventeen"
    elif n==18:
        return "Eighteen"
    elif n==19:
        return "Nineteen"

def Tens(n):
    if n==20:
        return "Twenty"
    elif n==30:
        return "Thirty"
    elif n==40:
        return "Fourty"
    elif n==50:
        return "Fifty"
    elif n==60:
        return "Sixty"
    elif n==70:
        return "Seventy"
    elif n==80:
        return "Eighty"
    elif n==90:
        return "Ninety"

n=int(input("Enter a number:"))
position=0
s=""
while (n!=0):
    position=position+1
    digit=n%10
    n=n//10
    if position==1:
        s=Ones(digit)+s
    elif position==2:
        s=Teens(digit)+s
    elif position==3:
        s=Ones(digit)+" Hundred "+s
   print(s)

you have divided by ten in previous loop, so 12 is 2 at round 2, isn't it?

Hi Tony,

Thank you for your reply. Honestly, I am feeling stupid now since I dont even understand why should I divide by ten in previous loop, this is the hint from my teacher. When I run the code, I could translate number in Ones, but not in Tens or Teens. The ERROR is: s=Ones(digit)+s
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

If "n" is not in the range 1-->9 it returns None (when the number entered is 10 for example). If the tens position is a "1" then it is a teens number and you would not print the ones position. Ditto for tens position when it is zero, for the numbers 100, 201 etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.