如何对学费每次上涨保持累计总数并将这些值相加?

0 投票
1 回答
881 浏览
提问于 2025-04-18 03:13

大学费用估算器

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
    increasedCost = calculateTuitionIncrease(tuitionCost,tuitionIncrease,years)

    #Calls function that calculates tuition increase
    overallCost = calculateTotalCost(terms,tuitionCost,creditHours,bookCost,roomAndBoard,scholarships)

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

main()

1 个回答

0

一般来说,运行总和或运行平均值是通过多次执行代码来计算的。不过,我在这里没有看到任何循环。

也许你的意思是想要计算每个学校学费的总和。

如果是这样的话,当你计算平均值时,实际上你已经快成功了。你需要创建一个变量,用来存储总费用,这样在从函数中获取值时就可以把它加起来。

当你从函数中得到结果时:

tuitionTotal = (tuitionCost1+tuitionCost2+tuitionCost3) * tuitionIncrease

看起来你可以稍微理顺一下代码的逻辑。我建议你先把大部分问题问清楚,然后再进行所有的计算。

像这样可能会有帮助:

tuitionCost = 0
COST_TEXT = "How much will you be paying per credit hour at school number "

...

for i in range(numberOfSchools):
    tuitionCost += int(input(COST_TEXT+str(i+1)+"?"))

...

tuitionAverage = tuitionCost / (i+1)
tuitionTotal = tuitionCost * tuitionIncrease

撰写回答