2017-05-14 64 views
1

我在嘗試完成作業代碼時遇到了問題...Python3如何乘以列表中前一項的列表中的所有順序項目?

「大多數學區的教師根據教學經驗的年數提供薪水,例如,在我校學區的初任教師可能會在第一年支付3萬美元,在第一年之後的每一年的經驗,最多10年後,老師會比前一年增加2%的收入。

編寫程序它以表格的形式爲學區的教師顯示薪資時間表,輸入數據是起薪,百分比增加和時間表中的年數,時間表中的每一行應包含年份編號和那年的工資。「

我開始了這項任務,覺得自己做得相當不錯(至少對我來說)智能編碼。但是隨着時間的推移,我一直在想出自己被困在哪裏,於是我開始質疑是否因爲我在'arkin錯了樹'這麼說。

這裏是我到目前爲止有:

startSalary = int(input("Please enter beginning salary: ")) 
    percentIncrease = (float(input("Please enter percentage increase: "))/100) 
    numberYears = list(range(1,(int(input("Please enter number of years in schedule: ")) + 1)) 

    ''' 
    x = percentIncrease 
    y = numberYears #LIST# 
    z = startSalary 
    ''' 
    def percentFunc(x,y,z): 
     for years in y: 
      y[0] = z #startSalary 
      y[1:] = z * x #percentIncrease 

我想numberYears分配[0]到startSalary後,依次分配剩餘的9個項目(這是正確地numberYears [1實現:]?)爲上一個列表項的值乘以percentIncrease。

我只是想說這全錯了嗎?預先感謝任何幫助!

+0

考慮添加作業標籤 –

+0

'itertools.accumulate()'可以實現這些一階關係。 – AChampion

+0

爲什麼'numberYears'列表? –

回答

1

我儘量保持你是如何開始的。並嘗試使用基本的Python,所以它是有道理的。

startSalary = int(input("Please enter beginning salary: ")) 
percentIncrease = (float(input("Please enter percentage increase: "))/100) 
numberYears = list(range(1,(int(input("Please enter number of years in schedule: ")) + 1))) 


def calculateSalary(startSalary, percentIncrease, numberYears): 
    for year in numberYears: 

    salaryInc = startSalary*percentIncrease 
    newSalary = startSalary+salaryInc 
    startSalary = newSalary 
    print("{} year salary is {:0.2f}".format(year, newSalary)) 

calculateSalary(startSalary, percentIncrease, numberYears) 
+0

謝謝你這樣做! – therealjayvi

+0

@therealjayvi不客氣,如果這是正確的答案,請標記爲已接受。謝謝 – mtkilic

0

每年興建一次的東西,如:

def percent_func(percent_per_year, years, start_salary): 
    table = [] 
    for i in range(years): 
     table.append((i+1, start_salary)) 
     start_salary += round(start_salary * percent_per_year/100.0) 
    return table 
0

希望的解決方案幫助。 numberOfYears應該是一個整數。

def percentFunc(x, y, z): 
    """ 
    x = percentIncrease 
    y = numberYears #SHOULD BE INTEGER# 
    z = startSalary 
    """ 

    result = [z] 

    for year in range(1, y): 
     last_salary = result[len(result) - 1] 
     result.append(last_salary + last_salary* x/100) 

    return result 

x, y, z = 2, 10, 30000 
print (percentFunc(x, y, z)) 
1

使用numberYears爲整數,那麼你可以使用用於計算複合稅相同的數學:

numberYears = int(input("Please enter number of years in schedule: ")) 
for i in range(1,numberYears): 
    print("year ", i, ", salary: ", startSalary*((1+percentIncrease)**(i-1))) 

>>> 
Please enter beginning salary: 3000 
Please enter percentage increase: 2 
Please enter number of years in schedule: 5 
year 1 , salary: 3000.0 
year 2 , salary: 3060.0 
year 3 , salary: 3121.2 
year 4 , salary: 3183.6240000000003 
相關問題