-3

This is what I have so far but I can't seem to get a code to work that let's me ask at the end of the calculation if they would like to make another calculation again.

#_function definitions_________________________________________________________________________________________________

def validate_pay_rate(pay_rate):
    while pay_rate < 7.5 or pay_rate > 18.25:
        print ("\nERROR: You entered an invalid pay rate...")
        pay_rate = float(input("Enter employee's pay rate: $"))
    else:
        return pay_rate

def validate_hours_worked(hours_worked):
    while hours_worked <= 0 or hours_worked > 40:
        print ("\nERROR: You entered an invalid amount of work hours...")
        hours_worked = float(input("Enter hours worked: "))
    else:
        return hours_worked

def calculate_gross_pay(pay_rate, hours_worked):
    print("Gross Pay = $" + "%.2f" % float(pay_rate * hours_worked))



#_main_________________________________________________________________________________________________________________
pay_rate = float(input("Enter employee's pay rate: $"))
validate_pay_rate(pay_rate)
hours_worked = float(input("Enter hours worked: "))
validate_hours_worked(hours_worked)
calculate_gross_pay(pay_rate, hours_worked)
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Can you be more specific about the issue you're encountering? Also, I believe those else statements in the functions are unnecessary. – AMC Jan 19 '20 at 01:32
  • I am very new to python and this is for class. I need to create a loop for this program that at the end of the program it ask if you would like to pretty much use the program again to calculate another payroll. I can't seem to get it working no matter how hard I try and I honestly really don't know what I am doing. I have this but it just repeats its self over again and does not repeat the whole program and I don't know what I am doing wrong – Amanda Coleman Jan 19 '20 at 01:42
  • while True: a = input("Another? Yes or No?") if a=="yes": main() continue elif a=="no": break else: print("End") – Amanda Coleman Jan 19 '20 at 01:44
  • Hmm, this question might be of use here: https://stackoverflow.com/q/23294658/11301900. – AMC Jan 19 '20 at 01:49
  • As an aside, it's probably best to have the functions return values, rather than printing directly. – AMC Jan 19 '20 at 01:53

2 Answers2

0

Put an loop around the main block. Something like this would work. You should of course do something nicer for the input but thats the minimal version.

#_main_________________________________________________________________________________________________________________
calculated = False
while not calculated or input("Redoo or something else (type y/n)") == "y":
    pay_rate = float(input("Enter employee's pay rate: $"))
    validate_pay_rate(pay_rate)
    hours_worked = float(input("Enter hours worked: "))
    validate_hours_worked(hours_worked)
    calculate_gross_pay(pay_rate, hours_worked)
    calculated = True
NameVergessen
  • 598
  • 8
  • 26
0

If i understod you correctly this is what you wanted.

#main_____________________________________________________________________________
while True:
    pay_rate = float(input("Enter employee's pay rate: $"))
    validate_pay_rate(pay_rate)
    hours_worked = float(input("Enter hours worked: "))
    validate_hours_worked(hours_worked)
    calculate_gross_pay(pay_rate, hours_worked)
    continue_ = input("Do you want to do this again? Y/N: ")
    if continue_ == "n" or continue_ == "N":
        break
    else:
        print() # Just to get a space between each input
GabbeHags
  • 101
  • 1
  • 2
  • 8
  • Instead of the `print()`, you could just add a newline at the start of the first `input()`. It would probably be cleaner. – AMC Jan 19 '20 at 02:32
  • You can't add `\n` to a `input()`, if this was not what you meant please tell me what you meant. @AMC – GabbeHags Jan 19 '20 at 03:34
  • I meant change the start of the loop from `float(input(“Enter...”))` to `float(input(“\nEnter...”))`. – AMC Jan 19 '20 at 03:37
  • I tried that but nothing happend in pycharm, so i thought it coulden't be done. – GabbeHags Jan 19 '20 at 13:45
  • That’s really strange, I’ll try it myself and report back. – AMC Jan 19 '20 at 16:52