Sbtien
Sbtien

Reputation: 13

Python Incorrect Values

I'm working on an assignment that implements while and for loops to calculate the cost of tuition each year for the next 5 years, when the initial amount is 8000 and increases 3% each year.

My program works, but I'm getting the wrong values when I actually calculate the projected tuition.

In
-------------------
tuition = 8000
increase = 0.03
tuition_total = 0

for year in range(0, 6):
    tuition += ((tuition * increase) * year)
    print(tuition, '\t', year)

Out
-------------------
    8000.00     0
    8240.00     1
    8734.40     2
    9520.49     3
    10662.95    4
    12262.39    5

According to the assignment written by my teacher, here are what the values are supposed to be:

In 1 year, the tuition will be 8240.00.

In 2 years, the tuition will be 8487.20.

In 3 years, the tuition will be 8741.82.

In 4 years, the tuition will be 9004.07.

In 5 years, the tuition will be 9274.19.

Are my operations off? Would appreciate any suggestions for what I should change. Thanks!

Upvotes: 0

Views: 75

Answers (1)

peachykeen
peachykeen

Reputation: 4411

You are super close. Ask yourself, why you are multiplying (tuition * increase) by year?

The year that the tuition increase happens should be independent of the increase itself. Thus your for loop should be of the form:

for year in range(0, 6):
    tuition += (tuition * increase)
    print(tuition, '\t', year)

This should give you the same answers that your teacher provided as well.

Upvotes: 1

Related Questions