In [5]:
#imports the clear_output command from the Ipython.display library
from IPython.display import clear_output
#imports the sleep command from the time library
from time import sleep

#creates a function called leave
def leave():
    #creates a variable called exitQuestion which is equal to the user's input 
    exitQuestion = str(input("would you like to try again? Y,y or N,n ==> "))
    #if the exitquestion is equal to Y or y then this function is run
    if exitQuestion == "Y" or exitQuestion == "y":
        #clears the screen of all outputs
        clear_output()
        #brings you to the main function
        main()
    #if exitQuestion is equal to N or n then this function is run
    elif exitQuestion == "N" or exitQuestion == "n":
        #prints the text inside the parenthese
        print("have a good day")
    #if all other options failed then the else function is run
    else:
        #runs the leave command
        leave()

#creates a function called main
def main():
    #attempts to run the code inside of it. if it fails then the except function is run
    try:
        #creates a variable called firstName which is equal to the user's input
        firstName = str(input("Please enter your first name ==> "))
        #creates a variable called lastName which is equal to the user's input 
        lastName = str(input("please enter your last name ==> "))
        #creates a variable called payRate which is equal to the number the user's inputs
        payRate = float(input("Please enter your pay per hour ==> "))
        #creates a variable called hoursWorked which is equal to the number the user's inputs
        hoursWorked = float(input("Please enter your total hours worked ==> "))
    #if the try function fails then this function is run
    except:
        #prints the text in the parenthese
        print("please try again")
        #waits three seconds before run next part of function
        sleep(3)
        #clears the screen
        clear_output()
        #brings you back to the main funtion
        main() 
    
    #creates a variable called grossPay which is equal to payRate times hoursWorked
    grossPay = payRate * hoursWorked
    #creates a variable called federalTax which is equal to grossPay times .1259
    federalTax = grossPay * .1259
    #creates a variable called stateTax which is equal grossPay times .0482
    stateTax = grossPay * .0482
    #creates a vaiable called socialSecurity which is equal to grossPay times .0765
    socialSecurity = grossPay * .0765
    #creates a variable called totalDeduction which is equal to both taxes and taxes and socialSecurity combined
    totalDeductions = federalTax + stateTax + socialSecurity
    #creates a variable called netPay which is equal to grossPay minus totalDeduction
    netPay = grossPay - totalDeductions
    
    #creates and writes in the file it creates
    paystub = open("{}-{}-paystub.txt".format(firstName, lastName), "w")
    #writes the text in  the parenthese into the document
    paystub.write("""
            Pay stub
    -------------------------
    Last name = {}
    First name = {}
    Hourly rate = ${:.2f}
    Hours worked = {:.2f}
    -------------------------
        Before deductions
    Total earned = ${:.2f}
    -------------------------
            Deductions
    Federal tax = ${:.2f}
    State tax = ${:.2f}
    Social security = ${:.2f}
    Total deductions = ${:.2f}
    -------------------------
         After deductions
    Net pay = ${:.2f}
    -------------------------""".format(lastName, firstName, 
    payRate, hoursWorked, grossPay, federalTax, stateTax, socialSecurity,
    totalDeductions, netPay)) 
    #.format adds the data from the variables list in the () into the {} in the order of the list
    #closes the file we just created 
    paystub.close() 
    #creates a variable called readFile which open the file we just made in read mode
    readFile = open("{}-{}-paystub.txt".format(firstName, lastName), "r")
    #clears the screen of all outputs
    clear_output()
    #prints out he file we just created
    print(readFile.read())
    #closes the file
    readFile.close()
    #tells the program to run the leave function
    leave() 
    
#runs the main function
main()
            Pay stub
    -------------------------
    Last name = Doe
    First name = John
    Hourly rate = $11.00
    Hours worked = 58.10
    -------------------------
        Before deductions
    Total earned = $639.10
    -------------------------
            Deductions
    Federal tax = $80.46
    State tax = $30.80
    Social security = $48.89
    Total deductions = $160.16
    -------------------------
         After deductions
    Net pay = $478.94
    -------------------------
would you like to try again? Y,y or N,n ==> n
have a good day
In [ ]:
 
In [ ]: