[Tutor] Positional Arguments
Jenny Allar
jennyallar at gmail.com
Fri Nov 1 19:43:25 CET 2013
Hi everyone,
I'm onto my next assignment and I think I have the basics together,
but I'm having issues passing values from one function to another. I'm
using Python 3.3.2 on Windows 7 and below is my assignment and code
with the error that's got me hung up.
Thanks in advance for your help.
Assignment: 6. Assignment (after Chapter 6)
Write a payroll program that pays time and a half for anything over 40
hours. This should have 3 functions in addition to main.
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 output should look something like the following:
Payroll Information
Pay rate
$10.00
Regular Hours
40
Overtime hours
20
Regular pay
$400.00
Overtime pay
$300.00
Total Pay
$700.00
My code:
#This program calculates pay for time and a half for hours worked over 49.
#This program uses three functions in addition to main to accomplish the task.
def main():
print()
print("This program calculates your payroll information.")
print()
gethours() #Call the function that gets the hours worked payrate
print()
calcandprint() # Call the function that calculates and prints the
information
calc_pay() # Call the function that calculates the total pay
#This function asks for and accepts the hours worked.
#
#Variable Type Purpose
#hrswrkd int hold for number
of hours worked
#payrate int hold for rate
of pay per hour
def gethours():
print('Please enter the number of hours worked.')
hrswrkd=int(input('The number must be at least 8 and no more than 86. '))
print()
while hrswrkd < 8 or hrswrkd > 86: #Validate the number of hours worked
print('Error --- The number of hours worked must be no less
than 8 and no more than 86.')
hrswrkd=int(input('Please try again. '))
print()
print('Please enter the rate of pay.')
payrate=int(input('The number must be at least $7.00 and no more
than $50.00. '))
while payrate < 7.00 or payrate > 50.00: #Validate the rate of pay
print('Error --- The rate of pay must be more than $7.00 and
no more than $50.00. ')
print()
return hrswrkd, payrate # Return values to main function
#This function calculates the number of straight time hours and over time hours
# and prints the information.
#
#Variable Type Purpose
#strthrs int hold
for hours paid at straight time
#overhrs int hold
for hours paid at time and a half
#
def calcandprint (hrswrkd, payrate):
if hrswrkd <= 40:
calc_pay(hrswrkd, payrate)
print('You have worked', hrswrkd, 'hours paid in straight time
for this pay period.')
print('You have worked 0 hours paid in overtime pay for this
pay period.')
else:
calc_pay(hrswrkd, payrate)
print('You have worked 40 hours paid in straight time for this
pay period.')
print('You have worked', hrswrkd - 40, 'hours paid in overtime
pay for this pay period.')
return hrswrkd, payrate
def calc_pay(hrswrkd, payrate):
#
#This function calculates the pay for hours worked earning straight time pay and
#pay for hours worked earning overtime pay, and caclulated the total pay.
#This function also returns the information to main.
#
#Variable Type Purpose
#regpay int hold for
regular time pay
#overtimepay int hold for overtime pay
#totalpay int hold for
total pay for time period
#
if hrswrkd <= 40:
regpay=hrswrkd * payrate
overtimepay=0
else:
overtimepay=(hrswrkd-40) * (payrate * 1.5)
regpay=40 * payrate
totalpay=regpay+overtimepay
return regpay, overtimepay, totalpay
print()
print(' Payroll Information')
print('Pay Rate: ',
format(payrate, '.2f'))
print('Regular Hours: ',
format(regpay // payrate, '.2f'))
print('Overtime Hours: ',
format(overtimepay // (payrate * 1.5), '.2f'))
print('Regular Pay: ',
format(regpay, '.2f'))
print('Overtime Pay: ',
format(overtimepay, '.2f'))
print('Total Pay: ',
format(totalpay, '.2f'))
main()
Error:
Traceback (most recent call last):
File "C:\", line 93, in <module>
main()
File "C:\", line 15, in main
calcandprint() # Call the function that calculates and prints the
information
TypeError: calcandprint() missing 2 required positional arguments:
'hrswrkd' and 'payrate'
Again, thank you.
More information about the Tutor
mailing list