Python programming, getting the same value over and over instead of it increasing
Question:
So I am working on assignment for college and am still very new to coding so here is the question. At one college, the tuition for a full-time student is $8,000 per semester. It has been announced that the tuition will increase by 3 percent each year for the next five years. Write a program with a loop that `displays the projected semester tuition amount for the next five years.
tuition_increase = 0.03
tuition = 8000
tuition_total = 0
years = 5
print('tuition_totaltyears')
print('--------------------')
for years in range (1, years + 1)
tuition_total = (tuition * tuition_increase) + tuition
print(tuition_total, '/t', years)
I keep getting the same value over and over instead of it increasing by the tuition increase for each year
Answers:
you don’t sum tuition_total
value to tuition_total
value calculated from the previous step try it on following way.
tuition_total = ((tuition * tuition_increase) + tuition) + tuition_total
or
tuition_total += (tuition * tuition_increase) + tuition
Here’s corrected one. First line after for
is the correct formula for calculating compound interest. Note, that I start indexing years for it from zero.
print()
changed to use .format()
in order to get tabulated output.
tuition_increase = 0.03
tuition = 8000
tuition_total = 0
years = 5
print('tuition_totaltyears')
print('--------------------')
for years in range (0, years ):
tuition_total = tuition * (1+tuition_increase)** years
print('{0:<15} {1}'.format(int(tuition_total), years+1))
will print
tuition_total years
--------------------
8000 1
8240 2
8487 3
8741 4
9004 5
You don’t need tuition_total
just assign back to tuition
, e.g.:
In []:
tuition_increase = 0.03
tuition = 8000
years = 5
print('tuitiontyears')
print('--------------------')
print(tuition, 't', 0)
for years in range (1, years + 1):
tuition *= (1+tuition_increase)
print(tuition, 't', years)
Out[]:
tuition years
--------------------
8000 0
8240.0 1
8487.2 2
8741.816 3
9004.07048 4
9274.1925944 5
As you can see you will need to fix your formatting.
You can also use itertools.accumulate()
to do these things:
In []:
tuition_increase = 0.03
tuition = 8000
years = 5
print('{:10}{}'.format('tuition', 'years'))
print('-'*20)
for year, tuition in enumerate(it.accumulate(it.repeat(tuition, years+1), lambda x, y: x*(1+tuition_increase))):
print('{:<10.2f}{}'.format(tuition, year))
Out[]:
tuition years
--------------------
8000.00 0
8240.00 1
8487.20 2
8741.82 3
9004.07 4
9274.19 5
Small change need to be done.
tuition_increase = 0.03
tuition = 8000
tuition_total = 0
years = 5
print('tuition_totaltyears')
print('--------------------')
for years in range (1, years + 1):
tuition = (tuition * tuition_increase) + tuition
print(tuition, '/t', years)
Here is the suggested solution from Tony Gaddis’ book, Starting out with Python (Pearson Revel):
tuition = 8000
for i in range(1, 6):
tuition *= 1.03
if i == 1:
print("In 1 year, the tuition will be $" + str(tuition) + ".")
else:
print("In "+str(i)+" years, the tuition will be $" + str(tuition) + ".")
Here’s how I did it and got it right.
tuition = 8240.00
tuition1 = 8000
rate = 0.03
for year1 in range(1, 2):
tuition1 = tuition1 * (1 + rate)
print(f"In {year1} year, the tuition will be ${tuition1:.2f}.")
for year in range (2, 6):
tuition *= (1 + rate)
print(f"In {year} years, the tuition will be ${tuition:.2f}.")
So I am working on assignment for college and am still very new to coding so here is the question. At one college, the tuition for a full-time student is $8,000 per semester. It has been announced that the tuition will increase by 3 percent each year for the next five years. Write a program with a loop that `displays the projected semester tuition amount for the next five years.
tuition_increase = 0.03
tuition = 8000
tuition_total = 0
years = 5
print('tuition_totaltyears')
print('--------------------')
for years in range (1, years + 1)
tuition_total = (tuition * tuition_increase) + tuition
print(tuition_total, '/t', years)
I keep getting the same value over and over instead of it increasing by the tuition increase for each year
you don’t sum tuition_total
value to tuition_total
value calculated from the previous step try it on following way.
tuition_total = ((tuition * tuition_increase) + tuition) + tuition_total
or
tuition_total += (tuition * tuition_increase) + tuition
Here’s corrected one. First line after for
is the correct formula for calculating compound interest. Note, that I start indexing years for it from zero.
print()
changed to use .format()
in order to get tabulated output.
tuition_increase = 0.03
tuition = 8000
tuition_total = 0
years = 5
print('tuition_totaltyears')
print('--------------------')
for years in range (0, years ):
tuition_total = tuition * (1+tuition_increase)** years
print('{0:<15} {1}'.format(int(tuition_total), years+1))
will print
tuition_total years
--------------------
8000 1
8240 2
8487 3
8741 4
9004 5
You don’t need tuition_total
just assign back to tuition
, e.g.:
In []:
tuition_increase = 0.03
tuition = 8000
years = 5
print('tuitiontyears')
print('--------------------')
print(tuition, 't', 0)
for years in range (1, years + 1):
tuition *= (1+tuition_increase)
print(tuition, 't', years)
Out[]:
tuition years
--------------------
8000 0
8240.0 1
8487.2 2
8741.816 3
9004.07048 4
9274.1925944 5
As you can see you will need to fix your formatting.
You can also use itertools.accumulate()
to do these things:
In []:
tuition_increase = 0.03
tuition = 8000
years = 5
print('{:10}{}'.format('tuition', 'years'))
print('-'*20)
for year, tuition in enumerate(it.accumulate(it.repeat(tuition, years+1), lambda x, y: x*(1+tuition_increase))):
print('{:<10.2f}{}'.format(tuition, year))
Out[]:
tuition years
--------------------
8000.00 0
8240.00 1
8487.20 2
8741.82 3
9004.07 4
9274.19 5
Small change need to be done.
tuition_increase = 0.03
tuition = 8000
tuition_total = 0
years = 5
print('tuition_totaltyears')
print('--------------------')
for years in range (1, years + 1):
tuition = (tuition * tuition_increase) + tuition
print(tuition, '/t', years)
Here is the suggested solution from Tony Gaddis’ book, Starting out with Python (Pearson Revel):
tuition = 8000
for i in range(1, 6):
tuition *= 1.03
if i == 1:
print("In 1 year, the tuition will be $" + str(tuition) + ".")
else:
print("In "+str(i)+" years, the tuition will be $" + str(tuition) + ".")
Here’s how I did it and got it right.
tuition = 8240.00
tuition1 = 8000
rate = 0.03
for year1 in range(1, 2):
tuition1 = tuition1 * (1 + rate)
print(f"In {year1} year, the tuition will be ${tuition1:.2f}.")
for year in range (2, 6):
tuition *= (1 + rate)
print(f"In {year} years, the tuition will be ${tuition:.2f}.")