I’m trying to set up a while loop that will ask the user for the employee name, hours worked and hourly wage until the user enters ‘DONE’. Eventually I’ll modify the code to calculate the weekly pay and write it to a list, but one thing at a time. The problem is once the main while loop executes once, it just stops. Doesn’t error out but just stops. I have to kill the program to get it to stop. I want it to ask the three questions again and again until the user is finished. Thoughts?
Please note that this is just an exercise and not meant for any real world application.
def getName():
"""Asks for the employee's full name"""
firstName=raw_input("\nWhat is your first name? ")
lastName=raw_input("\nWhat is your last name? ")
fullName=firstName.title() + " " + lastName.title()
return fullName
def getHours():
"""Asks for the number of hours the employee worked"""
hoursWorked=0
while int(hoursWorked)<1 or int(hoursWorked) > 60:
hoursWorked=raw_input("\nHow many hours did the employee work: ")
if int(hoursWorked)<1 or int(hoursWorked) > 60:
print "Please enter an integer between 1 and 60."
else:
return hoursWorked
def getWage():
"""Asks for the employee's hourly wage"""
wage=0
while float(wage)<6.00 or float(wage)>20.00:
wage=raw_input("\nWhat is the employee's hourly wage: ")
if float(wage)<6.00 or float(wage)>20.00:
print ("Please enter an hourly wage between $6.00 and $20.00")
else:
return wage
##sentry variables
employeeName=""
employeeHours=0
employeeWage=0
booleanDone=False
#Enter employee info
print "Please enter payroll information for an employee or enter 'DONE' to quit."
while booleanDone==False:
while employeeName=="":
employeeName=getName()
if employeeName.lower()=="done":
booleanDone=True
break
print "The employee's name is", employeeName
while employeeHours==0:
employeeHours=getHours()
if employeeHours.lower()=="done":
booleanDone=True
break
print employeeName, "worked", employeeHours, "this week."
while employeeWage==0:
employeeWage=getWage()
if employeeWage.lower()=="done":
booleanDone=True
break
print employeeName + "'s hourly wage is $" + employeeWage
The problem is that after the first loop,
employeeNameand the other variables will already have values, so your inner while loops will be skipped. This leads to the outer loop repeating infinitely without doing anything.I would just remove the inner while loops: you don’t really need them, because you already do validation inside
getHoursand the other functions. Another option is to reset the variable values at the start of the outer while loop.Some more things to improve (not related to this error):
In
getHoursandgetWage, you can just usewhile Trueinstead of the condition you have now. If the condition is false, you would have returned from the function already anyway.You need to catch
ValueErroringetHoursandgetWage, in case non-numeric data was entered.instead of
booleanDone==False, usenot booleanDone. Though if you remove the inner loops as I suggested, you don’t even need this boolean: Just break out of the loop when needed.