You don't use total_tuition_increaseSum
for anything other than that one print, so you may as well call sum
on it in the print line:
total_tuition_increase = "${:,.2f}".format(sum(total_tuition_increase))
I would also recommend you look more at splitting the code into functions. Functions usually make code easier to read, use and change. For example, you might put your input management into one function so it's a small self contained chunk. All it needs to do that's different is return
the resulting tuition cost instead of setting it directly. This is how it'd work:
def get_cost():
while True:
residency = raw_input('Please input your type of residency, I for in-state, O for out-of-state, and G for graduate: ')
try:
return RESIDENCY_COST[residency.upper()]
break
except KeyError:
print ('Please enter I, G or O ONLY.')
And you could call it like this:
cost_of_tuition = get_cost()
Similarly, you could then wrap your printing into a print_tuition_table
function. This one would need to take parameters, your two lists and total_tuition_increase
.
def print_tuition_table(academic_year, academic_year_inc, total_tuition_increase):
print('UNDERGRADUATE TUITION FOR THE NEXT FIVE YEARS ')
print('ACADEMIC YEAR TUITION INCREASE ')
print('------------- ------------ -------- ')
for i, year in enumerate(range(16, 21)):
print('{}-{} {} {}'.format(year + 2000, year + 1,
academic_year[i],
academic_year_inc[i]))
print('TOTAL TUITION INCREASE {}'.format(total_tuition_increase))
Of course, you could change this to be more re-usable now if you didn't hard code it as being five years. Instead, you could check the length of the academic_year
list and then just change the way the loop works slightly:
def print_tuition_table(academic_year, academic_year_inc, total_tuition_increase):
length = len(academic_year)
print('UNDERGRADUATE TUITION FOR THE NEXT {} YEARS '.format(length))
print('ACADEMIC YEAR TUITION INCREASE ')
print('------------- ------------ -------- ')
for i, year in enumerate(range(16, 16 + length)):
print('{}-{} {} {}'.format(year + 2000, year + 1,
academic_year[i],
academic_year_inc[i]))
print('TOTAL TUITION INCREASE ' + total_tuition_increase)
Then you could do the same with your calculation loop. It would need to take in cost_of_tuition
and return multiple values, but return multiple values is easy in Python. First, you wrap it in a function like this:
def calculate_fees(cost_of_tuition):
for _ in range(5):
intMath = cost_of_tuition * 0.03
tuition_increase.append(intMath)
fnlMath = intMath + cost_of_tuition
years.append(fnlMath)
cost_of_tuition = fnlMath
academic_year.append("${:,.2f}".format(fnlMath))
academic_year_inc.append("${:,.2f}".format(intMath))
total_tuition_increaseSum = sum(tuition_increase)
total_tuition_increase = "${:,.2f}".format(total_tuition_increaseSum)
return academic_year, academic_year_inc, cost_of_tuition
Then you can set those values like this:
academic_year, academic_year_inc, cost_of_tuition = calculate_fees(cost_of_tuition)
Also, you could easily expand this function to take a years parameter so that it doesn't have to be stuck with 5 years.
def calculate_fees(cost_of_tuition, years):
for _ in range(years):
And since our printing function checks the length of your list, now you just need to change one number to be able to adjust the whole output.
With all those functions defined, here's how you could call them to run the script:
cost_of_tuition = get_cost()
academic_year, academic_year_inc, cost_of_tuition = calculate_fees(cost_of_tuition)
print_tuition_table(academic_year, academic_year_inc, cost_of_tuition)
total_tuition_increaseSum
, is very bad form). \$\endgroup\$