我已經編寫了一個Python程序,我希望接受來自用戶的「y」輸入,並且如果用戶輸入「y」,則執行一些簡單的計算並打印結果。Python程序不會超出第一個用戶輸入
但是,即使輸入爲「y」,用戶輸入也不會超出 。請幫助我找到程序中的錯誤。
的代碼是:
#This function is to get the user to input the data needed to use in the rest of the program
#It should return the 3 variables name, hrs_wrkd, and payrate
def get_info(name,hrs_wrkd,payrate):
print('is this working?');
name = input('What is the last name of the employee?');
hrs_wrkd = float(input('How many hours did',name,' work last week?'));
payrate = float(input('How much does',name,' get paid?'));
return name,hrs_wrkd,payrate
#This function should be to calculate the employee's regular pay hours
#It accepts arguments from get_info
def calculate_reg_pay(hrs_wrkd,payrate):
reg_hrs = hrs_wrkd
reg_pay = reg_hrs * payrate
OT_hrs = 0
OT_pay = 0
return reg_hrs,reg_pay,OT_hrs,OT_pay
#This function should calculate the Overtime pay for the employee
#It accepts arguments from the get_info function as well
def calculate_OT_pay(hrs_wrkd,payrate):
reg_hrs = hrs_wkrd - 40
reg_pay = reg_hrs * payrate
OT_hrs = hrs_wrkd - reg_hrs
OT_pay = OT_hrs * (payrate * 1.5)
return reg_hrs,reg_pay,OT_hrs,OT_pay
#This function decides which calculation to use, either OT or regular pay
#It also accepts srguments from get_info
def calc_employee(hrs_wrkd,payrate):
if hrs_wrkd <= 40:
calculate_reg_pay(hrs_wrkd,payrate)
else:
calculate_OT_pay(hrswrkd,payrate)
#This function should print the single employee information after it was calculated
#It gets its arguments from the calc_employee function
def print_employee(reg_pay,OT_pay,name):
print(name,'earned $',format(reg_pay,'.2f'),' worth of regular pay and ',format(OT_pay,'.2f'),' in overtime this week.')
#This function is supposed to calculate the running total of the hours and pay for overtime and regular pay for the company
# It accepts its arguments from the calc_employee function also
def running_total(reg_hrs,reg_pay,OT_hrs,OT_pay,total_reg_hrs,total_reg_pay,total_OT_hrs,total_OT_pay):
total_reg_hrs = total_reg_hrs + reg_hrs
total_reg_pay = total_reg_pay + reg_pay
total_OT_hrs = total_OT_hrs + OT_hrs
total_OT_pay = total_OT_pay + OT_pay
#This function is supposed to print out the running total for the company, but I realized that it isnt in the proper position when called
def print_totals(total_reg_hrs,total_reg_pay,total_OT_hrs,total_OT_pay):
print('The total regular hours worked was',total_reg_hours)
print('The total regular pay was $',format(total_reg_pay,'.2f'))
print('The total overtime hours worked was',total_OT_hours)
print('The total overtime pay was $',format(total_OT_pay,'.2f'))
# So here I am defining the main loop that will activate everytime the user selects Yes
#It calls most of the other functions
def main_loop():
get_info
calc_employee
print_employee
running_total
#Here I am defining the main program where I put the loop control
def main():
loop_control = input("Would you like to enter an employee's name, payrate and hours? y to do so")
if loop_control == "y":
main_loop
else:
print_totals(total_reg_hrs,total_reg_pay,total_OT_hrs,total_OT_pay)
#Here we call the main function
main()
嗯這沒有發佈我會喜歡的方式。它的一個類和我只是在一個損失,我什麼都沒有錯 – user3763612
關於如何清理這篇文章的任何提示也將讚賞 – user3763612
請參閱http://stackoverflow.com/editing-help和http:// stackoverflow。 com/help/formatting –