HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Python tuition calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
tuitioncalculatorpython

Problem

I'm a student learning Python, I made this calculator that will tell you the tuition for the next 5 years at an increase of 3% per year. How can I improve on this and did I do anything that isn't best practice?
To revise my new version, Python tuition calculator 2.0

```
inState = 10000
outState = 24000
profGrad = 40000

years = []
tuitionIncrease = []

i = raw_input('Please input your type of residency, I for in-state, O for out-of-state, and G for graduate: ')

if i == 'I':
costOfTuition = inState
elif i == 'O':
costOfTuition = outState
elif i == 'G':
costOfTuition = profGrad

x = 0
while x != 5:
intMath = costOfTuition * 0.03
tuitionIncrease.append(intMath)
fnlMath = intMath + costOfTuition
years.append(fnlMath)
costOfTuition = fnlMath
x += 1

academicYear1 = "${:,.2f}".format(years[0])
academicYear2 = "${:,.2f}".format(years[1])
academicYear3 = "${:,.2f}".format(years[2])
academicYear4 = "${:,.2f}".format(years[3])
academicYear5 = "${:,.2f}".format(years[4])

academicYear1Inc1 = "${:,.2f}".format(tuitionIncrease[0])
academicYear1Inc2 = "${:,.2f}".format(tuitionIncrease[1])
academicYear1Inc3 = "${:,.2f}".format(tuitionIncrease[2])
academicYear1Inc4 = "${:,.2f}".format(tuitionIncrease[3])
academicYear1Inc5 = "${:,.2f}".format(tuitionIncrease[4])

totalTuitionIncreaseSum = sum(tuitionIncrease)
totalTuitionIncrease = "${:,.2f}".format(totalTuitionIncreaseSum)
4
print('UNDERGRADUATE TUITION FOR THE NEXT FIVE YEARS ')
print('ACADEMIC YEAR TUITION INCREASE ')
print('------------- ------------ -------- ')
print('2016-17 ' + academicYear1 + ' ' + academicYear1Inc1)
print('2017-18 ' + academicYear2 + ' ' + academicYear1Inc2)
print('2018-19 ' + academicYear3 + ' ' + academicYear1Inc3)
print('2019-20 ' + academicYear4 + ' ' + academicYear1Inc4)
print('2020-21 ' + academicYear5 + ' ' + academicYear1Inc5)
print('TOTAL TU

Solution

If you're currently learning Python you should probably look at Python 3
more so than Python 2.

There are only two violations for PEP8 (caught by the command-line tool,
pep8), that's very good; line 9 can be split in a multitude of ways to
go below the 80 character limit and there are more than one space before
the plus in the last line.

i = raw_input('Please input your type of residency, I for in-state, O for '
              'out-of-state, and G for graduate: ')


Also, constants should be uppercase with underscores, I'd say that
applies to inState etc.

IN_STATE = 10000
OUT_STATE = 24000
PROF_GRAD = 40000


That said, it would be easier to use a dictionary for this kind of
selection (BASE_TUITION[i]) - that might be a bit less descriptive
though. Btw. i isn't very descriptive either.

Personally, I'd also stick with one kind of quotation marks, but YMMV.

Now for the code and other conventions.

First, code would usually be put into functions to make it reusable. I
don't know if you're that far ahead, just to note that it would be nice
to split the input, processing and output parts into separate units.

There is no error handling for invalid input. If I enter foo I'd
expect a (helpful) error message from the program, not a NameError as
in the current state. For example, using the following loop, the
program will only handle valid input and print at least an indicator
that something went wrong:

costOfTuition = None
while costOfTuition is None:
    i = raw_input('Please input your type of residency, I for in-state, O for '
                  'out-of-state, and G for graduate: ')

    if i == 'I':
        costOfTuition = IN_STATE
    elif i == 'O':
        costOfTuition = OUT_STATE
    elif i == 'G':
        costOfTuition = PROF_GRAD
    else:
        print("Invalid input.")


The while loop would be more readable using for and range. Since
x isn't actually used, _ can be used instead.

for _ in range(5):
    intMath = costOfTuition * 0.03
    tuitionIncrease.append(intMath)
    fnlMath = intMath + costOfTuition
    years.append(fnlMath)
    costOfTuition = fnlMath


The assignments to academicYear1 to 5 should be a warning sign. If
that happens in code it's usually an indicator that it can be more
easily achieved with less variables (and less typing).

years = []
tuitionIncrease = []
academicYear = []
academicYear1Inc = []

for _ in range(5):
    intMath = costOfTuition * 0.03
    tuitionIncrease.append(intMath)
    fnlMath = intMath + costOfTuition
    years.append(fnlMath)
    costOfTuition = fnlMath
    academicYear.append("${:,.2f}".format(fnlMath))
    academicYear1Inc.append("${:,.2f}".format(intMath))


If you want it in separate loops, that's fine too. Same goes for the
sum call at the end. It's nicer to look at, so I kept it around.

Lastly, the formatting is mostly good with using format. The last
print statements can be written in a nicer way though.

All in all:

IN_STATE = 10000
OUT_STATE = 24000
PROF_GRAD = 40000

costOfTuition = None
while costOfTuition is None:
    residency = raw_input('Please input your type of residency, I for '
                          'in-state, O for out-of-state, and G for graduate: ')

    if residency == 'I':
        costOfTuition = IN_STATE
    elif residency == 'O':
        costOfTuition = OUT_STATE
    elif residency == 'G':
        costOfTuition = PROF_GRAD
    else:
        print("Invalid input.")

years = []
tuitionIncrease = []
academicYear = []
academicYear1Inc = []

for _ in range(5):
    intMath = costOfTuition * 0.03
    tuitionIncrease.append(intMath)
    fnlMath = intMath + costOfTuition
    years.append(fnlMath)
    costOfTuition = fnlMath
    academicYear.append("${:,.2f}".format(fnlMath))
    academicYear1Inc.append("${:,.2f}".format(intMath))

totalTuitionIncreaseSum = sum(tuitionIncrease)
totalTuitionIncrease = "${:,.2f}".format(totalTuitionIncreaseSum)

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,
                                                  academicYear[i],
                                                  academicYear1Inc[i]))

print('TOTAL TUITION INCREASE              ' + totalTuitionIncrease)

Code Snippets

i = raw_input('Please input your type of residency, I for in-state, O for '
              'out-of-state, and G for graduate: ')
IN_STATE = 10000
OUT_STATE = 24000
PROF_GRAD = 40000
costOfTuition = None
while costOfTuition is None:
    i = raw_input('Please input your type of residency, I for in-state, O for '
                  'out-of-state, and G for graduate: ')

    if i == 'I':
        costOfTuition = IN_STATE
    elif i == 'O':
        costOfTuition = OUT_STATE
    elif i == 'G':
        costOfTuition = PROF_GRAD
    else:
        print("Invalid input.")
for _ in range(5):
    intMath = costOfTuition * 0.03
    tuitionIncrease.append(intMath)
    fnlMath = intMath + costOfTuition
    years.append(fnlMath)
    costOfTuition = fnlMath
years = []
tuitionIncrease = []
academicYear = []
academicYear1Inc = []

for _ in range(5):
    intMath = costOfTuition * 0.03
    tuitionIncrease.append(intMath)
    fnlMath = intMath + costOfTuition
    years.append(fnlMath)
    costOfTuition = fnlMath
    academicYear.append("${:,.2f}".format(fnlMath))
    academicYear1Inc.append("${:,.2f}".format(intMath))

Context

StackExchange Code Review Q#106072, answer score: 2

Revisions (0)

No revisions yet.