gpt4 book ai didi

Python3 如何将列表中的所有连续项乘以列表中的前一项?

转载 作者:行者123 更新时间:2023-12-01 02:59:22 24 4
gpt4 key购买 nike

我在尝试完成作业代码时遇到问题...

“大多数学区的教师的工资都是根据他们的教学经验年数制定的。例如,mySchool 学区的新任教师第一年的工资可能为 30,000 美元。每年第一年之后,最多 10 年,教师的经验值将比之前的值增加 2%。

编写一个程序,以表格格式显示学区教师的工资表。输入的内容包括起薪、增幅百分比以及时间表中的年数。明细表中的每一行应包含年份编号和当年的工资。”

我开始了这项作业,感觉自己正在编写相当不错且(至少对我而言)智能的编码。但随着我花了很长时间才弄清楚自己陷入了困境,我开始怀疑这是否是因为我“找错了树”,可以这么说。

这是我到目前为止所得到的:

    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。

我的做法是错的吗?预先感谢您的帮助!

最佳答案

我尽量保持你开始时的方式。并尝试使用基本的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)

关于Python3 如何将列表中的所有连续项乘以列表中的前一项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43960103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com