对于 Python 编程和学习基础知识仍然很陌生,非常感谢任何帮助。
#Define Variables
# x= starting tuition
# y= tuition increae per year
# z = new tuition
#List known Variables
cost = int(25000)
increase = float(.025)
#Calculate the tuiton increase
tuition = cost * increase
#Calculate the cost increase over 5 years
#Initialize accumulator for test scores.
total = int(25000)
for x in range(5):
print('$', format(total+tuition, ',.2f'), sep='')
输出应该类似于: 第一年:25,000 美元 第二年:25,625 美元 第三年:26,276.63 美元 第 4 年:26,933.54 美元 第 5 年:27,606.88 美元
我在编写脚本时遇到问题,因此 25,000 美元增加 2%,然后 25,625 美元增加 2%,然后增加 2% 等于 26,276.63 美元等等,持续 5 年。
谢谢你的帮助!
请您参考如下方法:
python 的好选择!一些基础知识...
您不需要告诉 python 您的变量是什么类型,您只需初始化它们并在运行时由 python 解释;
cost = 25000
increase = 0.025
除此之外,您的逻辑/数学似乎有点不对劲——正如评论中提到的,您应该在循环内重新计算学费,因为增加的百分比取决于前一年。
cost = 25000
increase = 1.025
print(cost)
for i in range(5)
cost = cost * increase
print(f'${str(cost)}')
乘以 1.025 等同于说“将 2.5% 添加到当前值”。我正在使用格式化字符串进行打印(字符串之前的 f 表示)- 您可以将变量或表达式放在 {} 中,只要它们输出字符串(因此 str(cost) 将成本转换为要打印的字符串)。
