2

How do I pass the return statement from calculateTuitionIncrease into an array and then use that array when calculating the total cost?

Here's my code:

import math

def calculateTuitionIncrease(cost, increase, years):  
    #This function calculates the projected tuition increase for each year.  
    counter = 0  
    while counter <= years:  
        increasedCost = (cost)+(cost*increase)  
        return increasedCost  

def calculateTotalCost(terms,tuition,creditHours,books,roomAndBoard,scholarships):  
    #This function will calculate the total cost of all your expenses.  
    totalBookCost = (books*terms)  
    totalTuitionCost = (tuition*creditHours)*(terms)  
    totalRoomAndBoard =(roomAndBoard*terms)  
    totalCost = (totalBookCost+totalTuitionCost+totalRoomAndBoard)-(scholarships)  
    return totalCost


def main():

    #Variable declaration/initialization
    years = 0
    terms = 0
    numberOfSchools = 0

    tuitionCost1 = 0
    tuitionCost2 = 0
    tuitionCost3 = 0
    tuitionCost = 0

    bookCost = 0
    roomAndBoard = 0
    scholarships = 0

    tuitionIncrease = 0
    increasedCost = 0

    creditHours = 0
    overallCost = 0

    #User inputs
    years = int(input("Will you be going to school for 2, 4 or 6 years?"))

    #If-statements for if user will be going to multiple schools.
    if years == 4 or years == 6:
        numberOfSchools = int(input("How many schools do you plan on attending during this time?"))

    if numberOfSchools == 2:
        tuitionCost1 = int(input("How much will you be paying per credit hour at the first school you'll be attending?"))
        tuitionCost2 = int(input("How much will you be paying per credit hour at the second school you'll be attending?"))
        tuitionCost = (tuitionCost1+tuitionCost2)/(2) #Finds average tuition between schools & assigns it to a variable

    elif numberOfSchools == 3:
        tuitionCost1 = int(input("How much will you be paying per credit hour at the first school you'll be attending?"))
        tuitionCost2 = int(input("How much will you be paying per credit hour at the second school you'll be attending?"))
        tuitionCost3 = int(input("How much will you be paying per credit hour at the third school you'll be attending?"))
        tuitionCost = (tuitionCost1+tuitionCost2+tuitionCost3)/(3) #Finds average tuition cost between schools & assigns it to a variable

    else:
        tuitionCost = int(input("Please enter how much you will be paying per credit hour."))

    terms = (years*2)

    tuitionIncrease = float(input("Please enter the projected tuition increase per year in percentage form (ex. if increase is 7% enter .07)."))
    creditHours = int(input("On average, how many credit hours will you be receiving per term?"))
    roomAndBoard = int(input("Please enter what your price of room and board will be per term."))
    bookCost = int(input("Please enter what your average book cost will be per term."))
    scholarships = int(input("Please enter the total amount you will be recieving from grants and scholarships."))

    #Calls function that calculates tuition increase per year
    increasedCost = calculateTuitionIncrease(tuitionCost,tuitionIncrease,years)

    #Calls function that calculates the total cost.
    overallCost = calculateTotalCost(terms,tuitionCost,creditHours,bookCost,roomAndBoard,scholarships)

    print ("Your total estimated college cost is", overallCost)

main()
4
  • Are you using Python2 or Python3? I'm asking because I see you're using the input function. Commented Apr 18, 2014 at 2:55
  • Python 3. I am very new to Python. Am I using the input function wrong? Commented Apr 18, 2014 at 2:58
  • No, but you should add the python-3.x tag in case someone wants to answer with Python3 only code. Commented Apr 18, 2014 at 3:02
  • @slobiwan It looks like you are pretty new to working with lists in Python. Once you get the hang of it, you'll love it! I kept this answer specific to your question but there's a lot of ways you could generally take better advantage of loops, lists, and dictionaries in your code. Commented Apr 18, 2014 at 3:07

4 Answers 4

2

For starters, it looks like calculateTuitionIncrease should be returning a list, because currently it's returning a single value and the loop is wrong (it's not advancing at all):

def calculateTuitionIncrease(cost, increase, years):  
    # This function calculates the projected tuition increase for each year.  
    counter = 0
    answer = []
    while counter <= years:  
        increasedCost = (cost)+(cost*increase)  
        answer.append(increasedCost)
        counter += 1
    return answer

As for the second part of the question, it's not clear how you're supposed to "use that array when calculating the total cost", but surely you must iterate over the list returned by calculateTuitionIncrease and do something with each element, for each year - you should know how to do this, it must be part of the problem description you received.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you're trying to return multiple values from the calculateTotalCost function. You could try using a tuple, which is like a mini-array that you can create by listing some values inside parentheses.

For instance, in your code you could replace

  return totalCost

with

  return (totalCost, totalBookCost, totalTuitionCost, totalRoomAndBoard)

Comments

0

The short answer is that you would make an empty list before the loop, and then append new items to it inside of your loop. After the loop you can either return the list or do something else with the list.

def calculateTuitionIncrease(cost, increase, years):
    #This function calculates the projected tuition increase for each year.
    counter = 0
    # make an empty list
    # before you start the loop
    costsPerYear = []
    # you should use 'less than' here (or start the counter at 1)
    while counter < years:
        increasedCost = (cost)+(cost*increase)
        # append this cost to the end of the list
        costPerYear.append(increasedCost)
        # add one to the counter
        counter = counter + 1
    # once the loop is over, return the list
    return costsPerYear

Then you could sum this quantity:

adjustedTuitions = calculateTuitionIncrease(cost, increase, years)
totalTuitionCost = sum(adjustedTuitions)

Alternatively, you can return the sum of those estimates and use a for loop in place of the while loop.

def calculateTuitionIncrease(cost, increase, years):
    #This function calculates the projected tuition increase for each year.
    # make an empty list
    # before you start the loop
    costsPerYear = []
    for year in range(years):
        increasedCost = (cost)+(cost*increase)
        # append this cost to the end of the list
        costPerYear.append(increasedCost)
    # return the sum of those costs
    return sum(costsPerYear)

and use it like this:

totalTuitionCost = calculateTuitionIncrease(cost, increase, years)

Here's a tutorial on for loops and lists.

Also, you don't need to import the math module, because you're not using any of its special math functions.

2 Comments

Thanks, this helps a ton! I just started programming in Python a few weeks ago and it's my first programming language, so I've got quite a bit of learning ahead of me. I am still a little bit confused on how I would use the returned list to calculate the total cost. And yeah, I though I would need the math module to get the average, but I was wrong.
@slobiwan assuming it's just a list of numbers and you want to add them up, the built-in sum() function would do exactly that. Honestly you should just play around with loops and print statements to get a sense for how lists and loops work.
0

You can use yield instead of return and use that function as iterator. So that's the example:

def  calculateTuitionIncrease(cost, increase, years):
    #This function calculates the projected tuition increase for each year.
    counter = 0
    while counter <= years:
        counter += 1
        yield (cost)+(cost*increase)

print(sum(calculateTuitionIncrease(1000, 10, 5)))  # -> 66000

# the same but long way
sum = 0
for i in calculateTuitionIncrease(1000, 10, 5):
    sum += i
print(sum)  # -> 66000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.