简体   繁体   中英

How would I ask if they want to calculate again?

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM