2012-04-06 31 views
0

我剛寫了一個程序來計算員工的工資率。對我來說,該程序看起來很好,但是當我嘗試運行它時,我得到一個重新啓動,並且它不運行。我試過重新啓動Python GUI,但沒有運氣。這裏是該程序:程序不運行,只是重新啓動

def get_info(): 
    hours = int(input("How many hours did you work this week?",)) 
    while hours < 8 or hours > 86: 
     print('Error ---- you must work at least 8 hours and no more than 86 hours') 
     hours = int(input('Please enter your hours worked again:',)) 
    print() 
    rate = float(input("Please enter your pay rate: $",)) 
    while rate < 7.00 or rate > 50.00: 
     print("Error ---- pay rate cannot be less than $7.00 or greater than $50.00") 
     rate = float(input("Please re-enter your pay rate: $",)) 

    return hours, rate 

def calc_hours(num): 
    if num < 40: 
     reg_hours = num 
     overtime = 0 
    else: 
     reg_hours = 40 
     overtime = num - 40 

    return reg_hours, overtime 

def calc_pay(num1, num2, pay_rate): 
    regular_pay = num1 * pay_rate 
    overtime_pay = num2 * (pay_rate * 1.5) 
    total_pay = regular_pay + overtime_pay 

    return regular_pay, overtime_pay, total_pay 

def main(): 
    get_info() 
    calc_hours(hours) 
    cal_pay(reg_hours, overtime, rate) 

    print() 
    print ("      Payroll Information") 
    print() 
    print ("Pay Rate", format(rate, '14.2f')) 
    print ("Regular Hours", format(reg_hours, '10.2f')) 
    print ("Overtime Hours", format(overtime, '10.2f')) 
    print ("Regular Pay", format(regular_pay, '10.2f')) 
    print ("Overtime Pay", format(overtime_pay, '10.2f')) 
    print ("Total Pay", format(total_pay, '10.2f')) 

是的,圖表將是不可靠的。我一直無法成功運行它,所以它會順利出現。

+0

你可能想用'python'標記你的問題,'variable' – George 2012-04-06 04:03:10

回答

1
hours, rate = get_info() 
reg_hours, overtime = calc_hours(hours) 
regular_pay, overtime_pay, total_pay = calc_pay(reg_hours, overtime, rate) 

print() 
print ("      Payroll Information") 
print() 
print ("Pay Rate", format(rate, '14.2f')) 
print ("Regular Hours", format(reg_hours, '10.2f')) 
print ("Overtime Hours", format(overtime, '10.2f')) 
print ("Regular Pay", format(regular_pay, '10.2f')) 
print ("Overtime Pay", format(overtime_pay, '10.2f')) 
print ("Total Pay", format(total_pay, '10.2f')) 

首先,看看你的main():。你調用了get_info()函數,當函數完成時,它返回hours, rate,但是你沒有存儲結果。 (這是你的hours,rate),接下來的兩行也是如此。當你調用你的方法時,它會返回答案,你必須將它們存儲到一個變量中。

這些變化

hours, rate = get_info() 
reg_hours, overtime = calc_hours(hours) 
regular_pay, overtime_pay, total_pay = calc_pay(reg_hours, overtime, rate) 

最後的3行,有你寫什麼錯字calc_pay而不是cal_pay。所以修復它會使你的程序工作,這裏是輸出。

輸出

How many hours did you work this week?8 

Please enter your pay rate: $20 

        Payroll Information 

Pay Rate   20.00 
Regular Hours  8.00 
Overtime Hours  0.00 
Regular Pay  160.00 
Overtime Pay  0.00 
Total Pay  160.00 

讓我給你解釋一下這些賦值語句一樣。的形式是這樣的: variable = expression

  1. 在RHS表達正被評估(該值是一個存儲器地址)
  2. 在LHS

的存儲在該變量中的存儲器地址鏈接你可能會發現有幫助閱讀:Defining Functions

如果你想解決你的聊天,這裏是如何做到這一點。

pattern = '{0:15s} {1:4.2f}' 
print(pattern.format('Pay Rate', rate)) 
print(pattern.format('Regular Hours', reg_hours)) 
print(pattern.format('Overtime Hours', overtime)) 
print(pattern.format('Regular Pay', regular_pay)) 
print(pattern.format('Overtime Pay', overtime_pay)) 
print(pattern.format('Total Pay', total_pay)) 

輸出:

Pay Rate   20.00 
Regular Hours  20.00 
Overtime Hours  0.00 
Regular Pay  400.00 
Overtime Pay  0.00 
Total Pay   400.00 

說明:

pattern = '{0:15s} {1:4.2f}' 
# 0 mean the blank should be filled with the first argument, 
# the colon(:) specifies the formatting of the string/number. 
# s means to format a string, 15s means the string will be padded with spaces 
# so it will take up exactly 15 spaces, without the number, s just mean 
# use the string without any space padding 
# d means format an integer, 4d mean the integer will be padded with space 
# so it takes up exactly 4 spaces. f means float, and .2 mean 2 decimal point. 
相關問題