0

I have to calculate gross pay for an employee and display the results. Your program will accept the employee’s name, hours worked, and the employee’s pay rate. The program will need to calculate overtime as well. Overtime is defined as anything over 40 hours is paid at 1.5 times the regular pay. The program should print the employee’s name, the gross pay amount, and only if there was overtime, print the overtime pay amount as well. Finally, the program should repeat as necessary until the user enters a sentinel value.

print("Payroll Calculator")
EmployeesName = input("Please enter employees Name or 0 to quit:")
WeeklyHours = int(input("Please Enter Hours Worked:"))
PayRate = int(input("Please Enter Pay Rate:"))
print("Normal Pay Rate is:", 40 * PayRate)
if(WeeklyHours > 40):
    Overtime = PayRate * 1.5
    if(WeeklyHours > 40):
     print("Your Overtime Hours are:", WeeklyHours - 40)
     print("Your Overtime Rate is:", Overtime * 1.5)
    GrossPay = WeeklyHours * Overtime

print("Your Gross Pay is:", WeeklyHours * Overtime)

That is what I have and there is no loop in the program. I can't seem to get this thing figured out and I am going crazy over here. I just want someone to help break it down for me. Thanks!

  • 1
    What exactly from the above code is difficult for you to understand ? How does loops are coming into context ? – Rohit Mar 01 '19 at 13:01
  • 1
    Just put everything after the first line in a [`while True:`](https://wiki.python.org/moin/WhileLoop) loop. And add a `break` when they enter "0". – 001 Mar 01 '19 at 13:03
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Jack Moody Mar 01 '19 at 13:55

3 Answers3

1

What you can do is put all the code you want to repeat several times in a loop. It can look like:

    while True:
        # code you want to repeat
        if some_condition:
            break

or:

    flag = False
    while not flag:
        # code you want to repeat
        if some_condition:
            flag = True
Diana
  • 86
  • 6
0

You can use a for loop like this:

print("Payroll Calculator")

count = 2
for i in range(count):
    EmployeesName = input("Please enter employees Name :")
    WeeklyHours = int(input("Please Enter Hours Worked:"))
    PayRate = int(input("Please Enter Pay Rate:"))
    print("Normal Pay Rate is:", 40 * PayRate)
    if(WeeklyHours > 40):
        Overtime = PayRate * 1.5
        if(WeeklyHours > 40):
            print("Your Overtime Hours are:", WeeklyHours - 40)
            print("Your Overtime Rate is:", Overtime * 1.5)
    print("Your Gross Pay is:", WeeklyHours * Overtime)

    finish = input('Would you like to continue? (y/n) :')
    if finish == 'n':
        count = 0
    elif finish == 'y':
        count += 1

Plus, your variable GrossPay isn't being used.

Sebastian Dixon
  • 75
  • 1
  • 13
  • 3
    What is your `break` supposed to do? – Nordle Mar 01 '19 at 13:18
  • @MattB., break is a control flow statement used to come out of the while loop. i.e. whenever the commands above are executed, instead of jumping to the next iteration of the loop, the program execution jumps to the statement just below the break command. You can read more about break and continue statements at https://www.programiz.com/python-programming/break-continue. – JChat Mar 01 '19 at 13:24
  • 3
    @JoyjitChatterjee Sorry, I was being a bit mean. This break statement will have no effect as it is outside of any loop, that's what I was getting at ;) – Nordle Mar 01 '19 at 13:26
  • fixed, sorry about that guys, had just written up another comments code before, shouldve checked before post @MattB. – Sebastian Dixon Mar 01 '19 at 13:39
0

Instead of using while True, I would set the "sentinel variable" first to a non-breaking value. To make the teacher happy. Given e.g. the sentinel variable would be to write "0" as the EmployeeName, you would do

print("Payroll Calculator")
EmployeesName = None
while EmployeesName != '0': # 0 in python2
   EmployeesName = input("Please enter employees Name or 0 to quit:")
   If EmployeesName != '0':
       WeeklyHours = int(input("Please Enter Hours Worked:"))
       PayRate = int(input("Please Enter Pay Rate:"))
       print("Normal Pay Rate is:", 40 * PayRate)
       if(WeeklyHours > 40):
           Overtime = PayRate * 1.5
           if(WeeklyHours > 40):
               print("Your Overtime Hours are:", WeeklyHours - 40)
               print("Your Overtime Rate is:", Overtime * 1.5)
           GrossPay = WeeklyHours * Overtime
           print("Your Gross Pay is:", WeeklyHours * Overtime)

but you could also define some custom variable to handle this:

stop = False
while not stop:
   ...
   # check now if if we should stop
   if (EmployeesName == '0'):
       stop = True

You can always do the while True: break approach, but I would not recommend it, as the task asks for a variable in the while condition, and imho while True loops should be avoided until you really understand loops thoroughly.

while True:
   ...
   # check now if if we should stop
   if (EmployeesName == '0'):
       break # exits loop.

It makes sence in this example.

note: Python2 will return an integer for input(), Python3 a string. edited the example to be python3.

g4borg
  • 143
  • 2
  • 9
  • `EmployeesName` will be string not int (`input()`). So, `while` loop will never end – alberand Mar 01 '19 at 14:05
  • true, in python3 it will be str. in python2 it would be int. I actually had str first, then i corrected it on a test. however i used default python, which was py2. updated the example to use py3 as default (as in python2 the example would not work anyway for the name input). – g4borg Mar 02 '19 at 16:50
  • The example is incorrect anyway. When you will type '0' it will still ask you for hours/pay rate and will continue with calculations... – alberand Mar 02 '19 at 20:18
  • that is because this was the code written in the original post. that does not make it incorrect. I show how to use a while loop, not how to do the whole homework. – g4borg Mar 04 '19 at 07:55
  • also the example was to use EmployeeName (or any input) as sentinel variable with '0' as breaking condition. It will count as correct in any lecture. – g4borg Mar 04 '19 at 08:03