Good afternoon,
I have posted a similar question before. I am a Python Newbie. But, I really need some code help here. I am writing a payroll program. Here is my code:

employee=[]
eName="Employee"
pRate="Payrate"
hWork="Weekly Hours"
while (eName, pRate, hWork)>0:
    eName=raw_input("\nEnter the employees' first and last name. ")
    pRate=int (raw_input("What is their hourly wages? " ))
    if pRate < 6 or pRate > 20:
        print ("Employee has to make at least $6 and no more than $20!")
    hWork=int (raw_input("How many hours did they work this week? "))
    if hWork < 1 or hWork > 60:
        print ("Employee has to work at least 1 hour and no more than 60 hours!")

Now here is my question. I am trying to get the error messages to pop up right after they make and error in the hours worked of wages. I want them to ask the same question again instead of going to the next one when the user makes and error. My error messages show up. But it is going to the next input question instead of repeating the question. How can I make it repeat the same question? And this is an infinite loop. I am totally drawing a blank on how to end the loop. The loop is suppose to end when the user types in done.

This is by no means the cleanest way to implement this code. In order to complete what you are asking continue and break statements would mostly do the trick. If you include a continue statement after you print out an error message you will stop the next question from being asked. For instance:

employee=[]
eName="Employee"
pRate="Payrate"
hWork="Weekly Hours"
while True:
	eName=raw_input("\nEnter the employees' first and last name. ")
	pRate=int (raw_input("What is their hourly wages? " ))
	if pRate < 6 or pRate > 20:
        	print ("Employee has to make at least $6 and no more than $20!")
		continue
	hWork=int(raw_input("How many hours did they work this week? "))
    	if hWork < 1 or hWork > 60:
        	print ("Employee has to work at least 1 hour and no more than 60 hours!")
		continue
	break

When a continue is reached the code will restart from the top of the while loop. The only way that the code can reach the final break statement is if all entered values were valid.

You are not actually saving any of the inputs that the user is giving you. Just before the last break statement you need to update employee with the values that the user entered.

Also please make sure not to put spaces between function names and parentheses such as in 'int ('. I fixed this in the code that is supplied above. Python can be funny about such things. The code above works perfectly when run from the command line without the space and does not run properly on my machine with the space.

Thank you for your help. You were saying that in order to save the data you have to use a value command? I am not sure I follow. But, my school assignment actually requires me to not only save what they entered but to write out a sequential file then sort the data and read it back. I am pretty sure that I can do the sorting, but not sure what you mean by save the value.

I am slightly confused by your description of the assignment. You are using raw input to get values, so it is very difficult to get a large number of entries with such a scheme. In a real world setting you would have a file somewhere that would contain this information in some predefined format. From that you could read the data in and do whatever sorting or file writing that you desired.

As far as your code is concerned, you can save the information gathered into a list by doing the following after you exit the while loop:

employee = [eName, pRate, hWork]

You can then sort this list using built in methods which are well documented in the python help files. File I/O is also well documented and straightforward.

P.S. The first four lines of your code make your work look sloppy. The first line is never used and the next three are immediately overwritten so there is no point in assigning them. Python will retain references to eName, pRate, and hWork even though they are assigned in a code block (the while loop) unlike some other languages such as C++. That means that you will have access to these variables in any subsequent lines of code.

I am sorry I am not be specific enough. Do you recommend I use the def command. And maybe this will clear up what my assignment is. Basically I am suppose to write a payroll code. The user is suppose to input employee name, hourly wage, hours worked in the week; until the user types done, then the program is suppose to validate the information. After the information is validated the program is suppose to calculate the wages with overtime (if need be). After that is all done the information is suppose to be entered into a sequential file. Then the program is suppose to sort the list and print it on the screen in lowest, highest, and average weekly pay. I know that was a long explanation. But I hope it helps.

Functions are nice! You could write a function to check an employee's entries. At the end of your while loop you could construct the list of values for the employee and then run it through the function to check it. If it doesn't pass you can then print something saying that there was an error and not keep the data, otherwise you could append the employee's information list to a list. This would create a list of lists where each entry is a list of information for a single employee.

Once that is accomplished you can perform sorting and file I/O on the entries of the list of lists.

So you would recommend I use the def command which is technically the function command.

It would make your code more elegant if you first defined a function (which in this case would mean using a def keyword) that would take as input the list of entries for a single employee and would give as output a true or false statement. Your code would then check whether or not the output is true or false using an if statement. If true you would add the employee information to your records and if false you would not add it.

Also, I would use the print statements from your code and paste them into your function. That way the user can have some sort of intelligent feedback. So when the function runs it will signal your code in the while loop to either keep the data or not. If not then you will have hit one of the print statements inside the function that will notify the user of the error.

Thank you for the advice. Here is my new code. I think I made it more appealing. But I don't have the slightest clue how do set up the loop to continue until the user types in DONE. If you could help with that it would be much appreciated. Thank you.

def employee():
    global eName
    global pRate
    global hWork

raw_input("Hit ENTER to start Payroll Program.")
while True:
    eName=raw_input("\nPlease enter the employees' first and last name. ")
    hWork=int(raw_input("How many hours did they work this week? "))
    if hWork <1 or hWork >60:
        print "Invalid Weekly Hours!"
        continue
    pRate=int(raw_input("What is their hourly rate? "))
    if pRate <6 or pRate >20:
        print "Invalid Payrate!"
        continue
    break
This article has been dead for over six months. Start a new discussion instead.