Reputation: 313
Still very new to Python programming and learning the basics, any help is greatly appreciated.
#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='')
Output should be similar to: Year 1: $25,000 Year 2: $25,625 Year 3: $26,276.63 Year 4: $26,933.54 Year 5: $27,606.88
I am having trouble writing the script so that there is a 2% increase added onto $25,000, then 2% onto $25,625, then 2% increase to equal $26,276.63 etc. etc. for 5 years.
Thank you for the help!
Upvotes: 2
Views: 2203
Reputation: 141
replace the increased value which is %
#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
Upvotes: 0
Reputation: 321
What you're wanting to do is basically compound interest and the formula for that would be cost * (1+increase)^n
where n
would be the number of years so far
25000 * (1+0.025)^0 = 25000
25000 * (1+0.025)^1 = 25625
25000 * (1+0.025)^2 = 26265.62
25000 * (1+0.025)^3 = 26922.27
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='')
Hope this helps!!
Upvotes: 0
Reputation: 15872
You are just printing same value (increase in ) over and over. Try this:
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=' ')
Upvotes: 0
Reputation: 640
Good choice on python! A few basics...
You don't need to tell python what type your variables are, you just initialise them and python interprets at run time;
cost = 25000
increase = 0.025
Aside from that your logic/maths seems a little off - as mentioned in the comments you should be recalculating the tuition inside the loop, as the % increase depends on the previous year.
cost = 25000
increase = 1.025
print(cost)
for i in range(5)
cost = cost * increase
print(f'${str(cost)}')
Multiplying by 1.025
is the same as saying 'add 2.5% to the current value'. I'm using a formatted string to print (the f
before the string says that) - you can put variables or expressions inside the {}
, as long as they output strings (hence the str(cost)
which converts the cost to a string to print).
Upvotes: 1