从Python函数返回多个值

时间:2014-04-18 02:48:50

标签: python arrays return-value

如何将calculateTuitionIncrease的return语句传递给数组,然后在计算总费用时使用该数组?

这是我的代码:

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 个答案:

答案 0 :(得分:2)

对于初学者来说,看起来calculateTuitionIncrease应该返回一个列表,因为当前它返回一个值并且循环错误(它根本没有前进):

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

至于问题的第二部分,目前尚不清楚你在计算总成本时应该如何“使用该数组”,但肯定必须迭代calculateTuitionIncrease对每个元素做一些,每年 - 你应该知道如何做到这一点,它必须是你收到的问题描述的一部分。

答案 1 :(得分:0)

您似乎正在尝试从calculateTotalCost函数返回多个值。您可以尝试使用tuple,它就像一个迷你数组,您可以通过在括号内列出一些值来创建。

例如,在您的代码中,您可以替换

  return totalCost

  return (totalCost, totalBookCost, totalTuitionCost, totalRoomAndBoard)

答案 2 :(得分:0)

简短的回答是你会在循环之前创建一个空列表,然后在循环中向它添加新项。在循环之后,您可以返回列表或对列表执行其他操作。

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

然后你可以总结这个数量:

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

或者,您可以返回这些估算值的总和 并使用for loop代替while循环。

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)

并像这样使用它:

totalTuitionCost = calculateTuitionIncrease(cost, increase, years)

Here's a tutorial on for循环和列表。

此外,您不需要导入math module,因为您没有使用任何特殊的数学函数。

答案 3 :(得分:0)

您可以使用yield而不是return,并将该函数用作迭代器。这就是例子:

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