HENRY
HENRY

Reputation: 5

Python error calculating program

Ask the user to enter payroll information for the company. Set up a loop that continues to ask for information until they enter “DONE”. For each employee ask three questions:

name (first & last) hours worked this week (only allow 1 through 60) hourly wage (only allow 6.00 through 20.00) VALIDATE the hours worked and the hourly wage, and make sure a name is entered.

Calculate each employee’s pay, and write it out to a sequential file. Be sure to include file I/O error handling logic.

Include only the weekly pay Weekly pay is calculated: For (1-40 hours) it is hourly rate * hours worked For (41-60 hours) it is (hours worked – 40) * (hourly rate * 1.5) + hourly rate * 40

After all the employees are entered, read in the sequential file into a list named PAY for the weekly pay of each employee. Sort the list. Now print the lowest, highest, and average weekly pay for the week.

I am having obvious problem with this code

while len(eName)>0:
     eName=raw_input("\nPlease enter the employees' first and last name. ")
     hWork=raw_input("How many hours did they work this week? ")
     hoursWork=int(hWork)
     if hoursWork < 1 or hoursWork > 60:
         print "Employees' can't work less than 1 hour or more than 60 hours!"

     else:
         pRate=raw_input("What is their hourly rate? ")
         payRate=int(pRate)
         if payRate < 6 or payRate > 20:
              print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
         if hoursWork <=40:
              grossPay=hoursWork*payRate
         else:
              grossPay=((hoursWork-40)*(payRate*1.5))+(40*payRate)
         lsthours.append(grossPay)
     print grossPay
     print lsthours



     ePass=raw_input("Type DONE when finished with employees' information. ")
     ePass.upper() == "DONE"
     if ePass == "DONE":
          break
     else:
          continue

Upvotes: 0

Views: 1722

Answers (4)

pyCoder
pyCoder

Reputation: 501

Yu can do something as this:

grossPay = 0.0
lsthours = []

eName=raw_input("\nPlease enter the employees' first and last name (type 'PASS' to  exit): ") 

while eName.upper() != "PASS":      
   hWork=raw_input("How many hours did they work this week? ") 
   hoursWork=int(hWork)

   if hoursWork < 1 or hoursWork > 60: 
      print "Employees' can't work less than 1 hour or more than 60 hours!" 
   else: 
      pRate=raw_input("What is their hourly rate? ") 
      payRate=int(pRate) 

      if payRate < 6 or payRate > 20: 
         print "Employees' wages can't be lower than $6.00 or greater than $20.00!" 

      if hoursWork <=40: 
         grossPay=hoursWork*payRate 
      else: 
         grossPay=((hoursWork-40)*(payRate*1.5))+(40*payRate) 

      lsthours.append(grossPay) 

      print grossPay 
      print lsthours 

  eName=raw_input("\nPlease enter the employees' first and last name. (type 'PASS' to exit): ")

Upvotes: 1

rewritten
rewritten

Reputation: 16435

try this:

lsthours = list()
eName = "start" # initialize to something to start the loop
while eName:
    eName = raw_input("\nPlease enter the employees' first and last name. ")
    if not eName:
        break #loop will exit also when blank name is inserted
    hWork = raw_input("How many hours did they work this week? ")
    hoursWork = int(hWork)
    if hoursWork < 1 or hoursWork > 60:
        print "Employees' can't work less than 1 hour or more than 60 hours!"
        continue #skip

    pRate = raw_input("What is their hourly rate? ")
    payRate = int(pRate)
    if payRate < 6 or payRate > 20:
        print "Employees' wages can't be lower than $6.00 or greater than $20.00!"
        continue #skip
    if hoursWork <= 40:
        grossPay = hoursWork * payRate
    else:
        grossPay = ((hoursWork - 40) * (payRate * 1.5)) + (40 * payRate)
    lsthours.append(grossPay)
    print grossPay
    print lsthours
    ePass = raw_input("Type DONE when finished with employees' information. ")
    if ePass.upper() == "DONE":
        break

It still lacks exception checking but should work.

The "data error" checks should just short-circuit the main loop, it's simpler, but you can have a more involved code and put them into their own loop.

Upvotes: 1

pyfunc
pyfunc

Reputation: 66709

A few errors as has been pointed out:

In python, indentation decides the code blocks

while loop:

while logic_test:
    # this is inside while loop
    ....
# this is outside while loop

Certain functions on string does not replace the string in place, they return another string via return value

upper:

>>> a = "done"
>>> a.upper()
'DONE'
>>> a
'done'
>>> 

Always initialize your variables.

If you are using sequence methods, the variable should have been defined as sequence earlier.

>>> t.append('ll')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 't' is not defined
>>> t = []
>>> t.append('ll')
>>> 

Make your scope explicit

lsthours = []
while len(eName)>0:
    ........
    lsthours.append(grossPay)

Upvotes: 1

Malcolm Box
Malcolm Box

Reputation: 4036

There's several problems with this code:

  • The indentation is all over the place. For example, the while loop ends at that first if statement
  • The test for the while loop is almost certainly false (since eName isn't initialised), so the loop never enters
  • the code at ePass.upper() == "DONE" is trying to set the ePass variable, which means that test won't work. You need:

    if ePass.upper() == "DONE": break

Upvotes: 2

Related Questions