Ask Question
17 January, 15:04

Write a program that asks the user to enter the amount that he or she has / / budgeted for a month. Call a function that uses a loop that prompts the user / / to enter each of his or her expenses for the month and keep a running total. / / The loop finishes when the user enters an expense of 0 at which time the function / / returns the total for the monthly expenses. The program should display the amount / / that the user is over or under budget.

+4
Answers (1)
  1. 17 January, 15:33
    0
    The program is written in Python as it possesses a simple and clean syntax.

    def getExpenses () : total_amount = 0 amount = float (input ("Amount: $")) total_amount + = amount while (amount!=0) : amount = float (input ("Amount: $")) total_amount + = amount return total_amount budget = float (input ("Enter your monthly budget: $")) expenses = getExpenses () if (budget > expenses) : print ("Under budget.") else: print ("Over budget.")

    Explanation:

    Line 1 - 11: (A function to prompt user for each of his/her expenses)

    Create a variable total_amount to track the running total. Assign an initial value zero to the variable. (Line 2) Use Python built-in function input () to prompt user for expenses amount and assign the input to variable amount. (Line 4) Add the expenses amount to variable total_amount. (Line 5) Create a while loop to keep prompting user for expenses amount and add the expenses amount to variable total_amount until user input zero (Line 7-9) Return total_amount as output

    Line 13: Get user input for monthly budget

    Line 14: Call the function getExpenses () to obtain the total expenses from user

    Line 16 - 19: Determine if user is over budget or under budget

    Set an if condition - if budget is bigger than expenses, display "Under budget" (Line 17) else, display "Over budget" (Line 19)
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a program that asks the user to enter the amount that he or she has / / budgeted for a month. Call a function that uses a loop that ...” in 📙 Engineering if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers