Search code examples
pythonpython-3.7

Calculating Tuition Cost Increase Over 5 Years


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!


Solution

  • 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).