我在尝试完成作业的代码时遇到了麻烦......
"大多数学区的教师按照他们多年教学经验提供薪水的时间表获得报酬。例如,mySchool学区的初级教师可能在第一年获得30,000美元的报酬。对于第一年(最长10年)之后的每年经验,教师比之前的价值增加2%。
编写一个程序,以表格形式显示学区教师的工资表。输入是起始工资,增加百分比和计划中的年数。时间表中的每一行都应包含该年份的年份数和工资。"
我开始了这项任务,觉得我做得相当不错(至少对我来说)智能编码。但随着时间的推移它带我去弄清楚我在哪里,我已经开始质疑它是否是因为我是' m'吠叫错误的树'可以这么说。
这是我到目前为止所得到的:
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 :(得分: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
答案 1 :(得分: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)
答案 2 :(得分: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
答案 3 :(得分: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))
答案 4 :(得分:0)
用户输入要计算的薪金,增加百分比和年数。该代码将列表中的顺序项乘以列表中的前一项。
以0年为1的起始范围,以endyear + 1结束的终止
startSalary = int(input("Please enter beginning salary: "))
percentIncrease = (float(input("Please enter percentage increase: ")) / 100)
numberYears = int(input("Please enter number of years in schedule: "))
for i in range(1,numberYears+1):
print("{} year salary is {:0.2f}".format(
i,
startSalary * ((1 + percentIncrease) ** (i - 1))))