In: Computer Science
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget.
The output should display the monthly budget, the total expenses and whether the user is over or under budget. Use a empty return (enter key) to end the loop.
#variable declaration and initialization
totalAmount = 0
totalExpense = 0
while True:
#get user input
amount = input("Enter the amount that they have budgeted for the
month: ")
expense = input("Enter expense for the month: ")
if amount == '' and expense == '':
break
else:
totalAmount = totalAmount + int(amount)
totalExpense = totalExpense + int(expense)
#calcualte budget
budget = totalAmount - totalExpense
#display result
print("\nMonthly budget =", budget)
print("Total expense = ", totalExpense)
if budget<0:
print("Over budget")
else:
print("Under budget")
The screenshot of the above source code is given below:
OUTPUT:
Enter the amount that they have budgeted for the month:
300
Enter expense for the month: 200
Enter the amount that they have budgeted for the month: 200
Enter expense for the month: 20
Enter the amount that they have budgeted for the month:
Enter expense for the month:
Monthly budget = 280
Total expense = 220
Under budget