Jump to content

help with looping

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
For my Fundamentals of Programming class we have to write a simple payroll program. The program will ask the user to enter an employee's name, employee's hourly wage, and employee's hours worked. The user must be able to enter as many names as needed, and then when finished simply type 'done.'

We also have to include some basic validation to ensure the user enters an amount of hours, or pay, within a given range.

There's more to the program than that... but that's just where I am right now.

I typed this up rather quickly, but it is somewhat doing what I want.
while  True:
    employeeName = raw_input("Please enter the employee's  first and last name: \n")
    if len(employeeName) == 0:
         print "You entered nothing.\n"
        continue
    else:
         workedHours = int(raw_input("How many hours did the employee work?\n"))
         if workedHours < 1:
            print "Employees can't work less  than 1 hour per week. Please try again."
            continue
         elif workedHours > 60:
            print "Employees can't work  more than 60 hours per week. Please try again."
            continue
         else:
            payRate = float(raw_input("What is the employee's  hourly wage?\n"))
            if payRate < 6.00:
                 print "Employee must earn no less than $6.00 an hour. Please try  again."
                continue
            elif payRate >  22.00:
                print "Employee must earn no more than $22.00  an hour. Please try again."
                continue
What it does do:
Does ask for an employee name.
Does ask for hours worked, and makes sure they're within range.
Does ask for hourly wage, and makes sure it's within the correct range.

However, as it is right now, if the wrong info is entered it just starts back asking the employee name. I'd like to re-ask for whatever was out of range instead.

I guess I'm looking more for pointers and some tips, and not someone to recode this for me.

Thanks!
Posted Image

#2
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Edit: Just realise how horrible the advice I tried to post there was, Ill have a think on it and reply in a bit...

Have a look at the Loops and Conditionals part of this little online book. Its down in page 7. May have something similar to what you're looking for.

#3
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
Thanks for the link... I'll give it a look later tonight when I have time. I was hoping the 'continue' would do the job, but it kicks up to the beginning of the while loop, and not the begging of the if statement.
Posted Image

#4
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Yeah I could see what you were trying. How about taking out the continues all together and re-asking the user to enter the value inside the if statement?

#5
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
I thought of that too, but I want to avoid redundant coding. For now I've decided not to worry about this, since it isn't required for the assignment.

If anyone does know how to accomplish this feel free to post up. I'd still like to know if it can be done, or if due to the nature of the while Loop it can't be done the way I was asking.

For now this is what my loop looks like:
payRoll = True

while payRoll:
    employeeName = raw_input("Please enter the employee's first and last name: \n")
    employeeName = employeeName.title()
    if employeeName == "Done":
        payRoll = False
    elif len(employeeName) == 0:
        print "You entered nothing.\n"
    else:
        workedHours = int(raw_input("How many hours did the employee work?\n"))
        if workedHours < 1 or workedHours > 60:
            print "Employees can only work between 1 and 60 hours. Please try again."
        else:
            payRate = float(raw_input("What is the employee's hourly wage?\n"))
            if payRate < 6.00 or payRate > 22.00:
                print "Employee can only earn between $6.00 and $22.00 an hour. Please try again."
            else:
                #will create an instance of my class here

Now I'm going to work on the class object that will store all the values, and the methods to calculate them.

Edited by Root23, 30 April 2010 - 09:46 PM.

Posted Image

#6
Guest_x42_*

Guest_x42_*
  • Guests
alright it looks like you are having it check the input after it is given to see if it is acceptable, and if it isn't you put "continue" some languages will go back to the start of the loop when they read the word continue, so instead try:

first_input=""
first_input_error=True
while (True):
while (First_input_error):
first_input=input("input one")
if (first_input==ok):
first_input_error=False

and do that for every input, by the way, can someone please tell me how you get the code inside those boxes?

#7
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts

x42 said:

can someone please tell me how you get the code inside those boxes?

Just encase it between code tags by clicking the # sign. There are also two you can use for HTML specific or PHP, they're to the right of the # sign.


As for the loop.. the last iteration of the loop I posted (post #5) is basically what I ended up using for the assignment, and it worked well. The only change I made was somewhat obvious.. I created the instance of the class before the loop. Then at the end of the loop I passed in all of the data to that object.
Posted Image

#8
CatatonicMan

CatatonicMan

    Newbie

  • Members
  • PipPip
  • 13 posts
Here's a quick and dirty way of doing what you originally wanted. I basically just threw the individual bits into individual loops that would run until a correct input was given.

You could also probably do it with some definitions and recursive calls, though that would certainly be overkill.

Either way, there's probably an easier way of doing it that I didn't think of.


payRoll = True


while payRoll:


    while True:

        employeeName = raw_input("Please enter the employee's first and last name: \n").title()

        if employeeName == 'Done':

            payRoll = False

            break


        if employeeName == '':

            print "You entered nothing.\n"

            continue


        break


    while True and payRoll:

        workedHours = int(raw_input("How many hours did the employee work?\n"))

        if not 1 < workedHours < 60:

            print "Employees can only work between 1 and 60 hours. Please try again.\n"

            continue


        break


    while True and payRoll:

        payRate = float(raw_input("What is the employee's hourly wage?\n"))

        if not 6.00 < payRate < 22.00:

            print "Employee can only earn between $6.00 and $22.00 an hour. Please try again.\n"

            continue


        break


    if payRoll:

        print 'Class Goes Here.\n'