SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Simple Functions
Exercise:
Programming Exercises
Question:4 | ISBN:9780132576376 | Edition: 2

Question

Automobile Costs
Write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and the total annual cost of these expenses.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Automobile Costs Python code:

#The 'getExpenseInput' is used to  asks the user 
#to enter the monthly costs for loan payment, 
#insurance, gas, oil, tires, and maintenance
def getExpenseInput():
    amount = []
    amount.append(float(input("Enter monthly loan payment: ")))
    amount.append(float(input("Enter monthly insurance cost: ")))
    amount.append(float(input("Enter monthly gas cost: ")))
    amount.append(float(input("Enter monthly oil cost: ")))
    amount.append(float(input("Enter monthly tires cost: ")))
    amount.append(float(input("Enter monthly maintenance cost: ")))
    return amount
#The 'computeExpenseTotal' is used to the 
#total monthly cost of these expenses, and
#the total annual cost of these expenses
def computeExpenseTotal(expenses):
    monthlyTotal = sum(expenses)
    annualTotal = monthlyTotal * 12
    return monthlyTotal, annualTotal
#main method
def main():
    #Call the methods and display output
    expenses = getExpenseInput()
    monthlyCost, annualCost = computeExpenseTotal(expenses)
    print("Total monthly cost: $", format(monthlyCost, ".2f"))
    print("Total annual cost: $", format(annualCost, ".2f"))
if __name__ == "__main__":
    main()

Executed Output:

Enter monthly loan payment: 21000
Enter monthly insurance cost: 5500
Enter monthly gas cost: 1200
Enter monthly oil cost: 2000
Enter monthly tires cost: 500
Enter monthly maintenance cost: 20000
Total monthly cost: $ 50200.00
Total annual cost: $ 602400.00

 

0 0

Discussions

Post the discussion to improve the above solution.