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:
Repetition Structures
Exercise:
Programming Exercises
Question:3 | ISBN:9780132576376 | Edition: 2

Question

Budget Analysis
Write a program that asks the user to enter the amount that he or she has budgeted for a month. A loop should then prompt the user to enter each of his or her expenses for the month, and keep a running total. When the loop finishes, the program should display the amount that the user is over or under budget.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Budget analysis program code:

#Prompt and read the monthly budget
monthBudget = float(input("Enter amount for your monthly budget: "))
#Initialize the variables
expens= 0
#A loop should then prompt the user to
#enter each of his or her expenses for 
#the month, and keep a running total
while True:
    expenditureAmount = input("Enter an expense amount(or 'E' to Exist): ")
    if expenditureAmount == "E":
        break
    expens += float(expenditureAmount)
#Calculate the budget
amount = monthBudget - expens

#Display the amount that the user is over or under budge
if amount >= 0:
    print("You are under budget by $", amount)
else:
    print("You are over budget by $", abs(amount))

Output 1:

Enter amount for your monthly budget: 60000
Enter an expense amount(or 'E' to Exist): 25000
Enter an expense amount(or 'E' to Exist): 3000
Enter an expense amount(or 'E' to Exist): 15000
Enter an expense amount(or 'E' to Exist): E
You are under budget by $ 17000.0

Output 2:

Enter amount for your monthly budget: 50000
Enter an expense amount(or 'E' to Exist): 40000
Enter an expense amount(or 'E' to Exist): 20000
Enter an expense amount(or 'E' to Exist): E
You are over budget by $ 10000.0

 

0 0

Discussions

Post the discussion to improve the above solution.