2014-10-29 51 views
0

我有一個關於我的Python程序的問題。我無法在這個程序中找到總數和總數。休息工作正常。有什麼辦法可以解決這個問題嗎?我很快就有了這個計劃。我會很感激任何提示我可以得到:d感謝Python重複結構和文件

midSalary = 50000 
maxSalary = 60000 

def main(): 

    inFile = open('program7.txt', 'r')  
    lineRead = inFile.readline() 
    total = 0.0 
    ntotal = 0.0 
    count = 0 

    while lineRead != '': 
     words = lineRead.split() 
     for word in words: 
      num = float(word) 
      total += num 
      count += 1   
      print("\nFaculty Member # ",count, ": $" , format(num, '.2f'), sep ="") 
      if num >= maxSalary: 
       payIncrease(num, .04) 
      elif num >= midSalary: 
       payIncrease(num, .07) 
      else: 
       payIncrease(num , .055) 
     lineRead = inFile.readline() 
    #averagePayRaise = (ntotal - total)/count 
    inFile.close() 
    for divider in range(45): 
     print("-", end ='') 
    print("\nTotal Faculty payroll : $", format(total , ",.2f"),sep ="") 
    print("The New Total Faculty payroll : $", format(ntotal , ",.2f"),sep ="") 
    print("Average Pay Raise : $", format(averagePayRaise, ",.2f"), sep ="") 

def payIncrease(amount, prcnt): 
    print("Pay Raise Percent : ", format(prcnt*100, ".1f")+"%") 
    total = 0.0 
    ntotal = 0.0 
    count = 0 
    salRaise = amount * prcnt 
    newSal = amount + salRaise 
    print("Pay Raise : $", format(salRaise, ',.2f'), sep ="") 
    print("New Salary : $", format(newSal, ',.2f'), sep = "") 
    total += amount 
    count += 1 
    ntotal += newSal 
    averagePayRaise = (ntotal - total)/count 

main() 
+0

我得到的解決方案爲0爲em – spuriousarbiter 2014-10-29 23:01:44

+0

您能顯示輸入,預期輸出和當前輸出嗎? – 2014-10-29 23:04:38

+0

共有教師工資:$ 1,310,555.15 新的總工資單系:$ 0.00 平均加薪:$ -59,570.69 我得到下面說 – spuriousarbiter 2014-10-29 23:06:45

回答

0

默認情況下,分配給在Python的名字是當地在聲明它們的功能。所以,如果您有:

def main(): 
    total = 0 

def payIncrease(): 
    total = 0 
    total += amount 

那麼你有命名total獨立的值。解決方法之一是讓全球:

total = 0 

def main(): 
    global total 
    total = 0 

def payIncrease(): 
    global total 
    total += amount 

請注意,你不希望分配total = 0payIncrease(),因爲那樣會跑步的總重爲0。你可能補充說,當你打破了你代碼到payIncrease()函數中,因爲Python沒有給你一個錯誤。

+0

我必須讓ntotal一個全局變量以及修復錯誤? – spuriousarbiter 2014-10-29 23:12:16

+0

你覺得怎麼樣?如果你不這樣做,它會起作用嗎? – 2014-10-29 23:12:39

+0

我需要再次查看我的代碼。我得到的是平均加薪的負數,我的ntotal打印的是最後一個newSal – spuriousarbiter 2014-10-29 23:18:49

0
def payIncrease(salary): 
    if current_sal >= maxSalary: return 0.04 
    if current_sal >= midSalary: return 0.07 
    return 0.055 

def processSalaryData(line): 
    """take a line and return current salary and new salary (note that a line is actually just a float)""" 
    try: 
     current_sal = float(line) 
    except ValueError: 
     return None 

    return current_sal,current_sal + current_sal*payIncrease(current_sal) 

def main(): 
    salaryData = [processSalaryData(line) for line in open("salaries.txt")] 
    salaryData = filter(None,salaryData) #filter out invalid data 
    adjustments = [b-a for a,b in salaryData] 
    old_salaries,new_salaries = zip(*salaryData) 

    print "OLD TOTAL PAYROLL :",sum(old_salaries) 
    print "NEW TOTAL PAYROLL :",sum(new_salaries) 
    print "AVG ADJUSTMENT:",sum(adjustments)/len(adjustments) 
    print "MAX SALARY:",max(new_salaries) 
    print "MIN SALARY:",min(new_salaries) 
+0

都能跟得上我寫這一次我我的代碼更新的答案之一。我試圖通過調用另一個函數來簡化我的代碼。否則它工作得很好 – spuriousarbiter 2014-10-29 23:05:25

+0

謝謝你!這也適用。希望我可以像你一樣編寫代碼蟒蛇大師!有一天,我猜大聲笑 – spuriousarbiter 2014-10-29 23:27:16