计算 5 年的学费增长
Calculating Tuition Cost Increase Over 5 Years
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
除此之外,您的 logic/maths 似乎有点偏离 - 正如评论中所述,您应该重新计算循环内的学费,因为增加的百分比取决于前一年。
cost = 25000
increase = 1.025
print(cost)
for i in range(5)
cost = cost * increase
print(f'${str(cost)}')
乘以 1.025
等同于计算 'add 2.5% to the current value'。我正在使用格式化的字符串来打印(字符串之前的 f
) - 你可以将变量或表达式放在 {}
中,只要它们输出字符串(因此 str(cost)
将成本转换为要打印的字符串)。
替换增加的值 %
#Define Variables
# x= starting tuition
# y= tuition increae per year
# z = new tuition
#List known Variables
cost = int(25000)
increase = float(0.02)
#Calculate the tuiton increase
tuition = cost * increase
#Calculate the cost increase over 5 years
#Initialize accumulator for test scores.
total = int(25000)
count = 1
"""use this to print in new line"""
for x in range(5):
print('year {}: ${} ' .format(count, total + count * tuition, ',.2f'), sep='')
count = count + 1
"""use this to print in same line"""
for x in range(5):
print('year {}: ${}, ' .format(count, total + count * tuition, ',.2f'), end='')
count = count + 1
您只是一遍又一遍地打印相同的值(增加)。试试这个:
cost = int(25000)
increase = float(1.025)
#Calculate the tuiton increase
#Calculate the cost increase over 5 years
#Initialize accumulator for test scores.
total = cost
print('Year 1: ${0:,.2f}'.format(total), end = ' ')
for x in range(2,6):
total = total*increase
print('Year {0}: ${1:,.2f}'.format(x,total), end=' ')
你想做的基本上是复利,公式是cost * (1+increase)^n
,其中n
是年数到目前为止
- 所以第 0 年
25000 * (1+0.025)^0 = 25000
- 所以第 1 年
25000 * (1+0.025)^1 = 25625
- 所以第 2 年
25000 * (1+0.025)^2 = 26265.62
- 所以第 3 年
25000 * (1+0.025)^3 = 26922.27
- 所以第 4 年
25000 * (1+0.025)^4 = 27595.32
cost = int(25000)
increase = float(.025)
for x in range(5):
tuition = cost*((1.000+increase)**x)
print(str(x) + ': $', format(tuition, ',.2f'), sep='')
希望对您有所帮助!!
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
除此之外,您的 logic/maths 似乎有点偏离 - 正如评论中所述,您应该重新计算循环内的学费,因为增加的百分比取决于前一年。
cost = 25000
increase = 1.025
print(cost)
for i in range(5)
cost = cost * increase
print(f'${str(cost)}')
乘以 1.025
等同于计算 'add 2.5% to the current value'。我正在使用格式化的字符串来打印(字符串之前的 f
) - 你可以将变量或表达式放在 {}
中,只要它们输出字符串(因此 str(cost)
将成本转换为要打印的字符串)。
替换增加的值 %
#Define Variables
# x= starting tuition
# y= tuition increae per year
# z = new tuition
#List known Variables
cost = int(25000)
increase = float(0.02)
#Calculate the tuiton increase
tuition = cost * increase
#Calculate the cost increase over 5 years
#Initialize accumulator for test scores.
total = int(25000)
count = 1
"""use this to print in new line"""
for x in range(5):
print('year {}: ${} ' .format(count, total + count * tuition, ',.2f'), sep='')
count = count + 1
"""use this to print in same line"""
for x in range(5):
print('year {}: ${}, ' .format(count, total + count * tuition, ',.2f'), end='')
count = count + 1
您只是一遍又一遍地打印相同的值(增加)。试试这个:
cost = int(25000)
increase = float(1.025)
#Calculate the tuiton increase
#Calculate the cost increase over 5 years
#Initialize accumulator for test scores.
total = cost
print('Year 1: ${0:,.2f}'.format(total), end = ' ')
for x in range(2,6):
total = total*increase
print('Year {0}: ${1:,.2f}'.format(x,total), end=' ')
你想做的基本上是复利,公式是cost * (1+increase)^n
,其中n
是年数到目前为止
- 所以第 0 年
25000 * (1+0.025)^0 = 25000
- 所以第 1 年
25000 * (1+0.025)^1 = 25625
- 所以第 2 年
25000 * (1+0.025)^2 = 26265.62
- 所以第 3 年
25000 * (1+0.025)^3 = 26922.27
- 所以第 4 年
25000 * (1+0.025)^4 = 27595.32
cost = int(25000)
increase = float(.025)
for x in range(5):
tuition = cost*((1.000+increase)**x)
print(str(x) + ': $', format(tuition, ',.2f'), sep='')
希望对您有所帮助!!