2014-06-21 52 views
-5

我已經編寫了一個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() 
+0

嗯這沒有發佈我會喜歡的方式。它的一個類和我只是在一個損失,我什麼都沒有錯 – user3763612

+0

關於如何清理這篇文章的任何提示也將讚賞 – user3763612

+0

請參閱http://stackoverflow.com/editing-help和http:// stackoverflow。 com/help/formatting –

回答

1

您需要通話的功能。這確實只是引用該函數的對象:

main_loop 

添加()調用一個函數:在你的main_loop()功能

main_loop() 

一樣:

def main_loop(): 
    get_info() 
    calc_employee() 
    print_employee() 
    running_total() 

倒不是說會工作,因爲你的功能需要參數;你需要在函數調用中傳入這些函數才能工作。 get_info()將參數忽略;刪除這些參數的函數簽名:

def get_info(): 
    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 

接下來,你要分配返回值; get_info()返回了一些東西,但你永遠不指定任何人:

name, hours_worked, payrate = get_info() 

現在,你可以通過這些來其他函數調用:

calc_employee(hours_worked, payrate) 

你必須退出一些更多的修復,這是超出了這個答案的範圍,但。不要忘記在函數調用中使用return!例如,calc_employee不會返回任何內容,例如,它只是忽略它委託給它返回的功能。

+0

那麼做了,現在至少我得到了一個回溯錯誤:D,謝謝 – user3763612

+0

@ user3763612:那你就更進一步了。儘管如此,那裏還有很多其他的錯誤。 –

+0

是的,有。我知道else語句開始了,我還沒有想出如何解決這個問題,但是如果我能讓剩下的部分運行,也許這會讓我更加接近。我們不應該使用任何全局變量 – user3763612

相關問題