How to create for loop for monthly budget program?

Question:

I am trying to create a for loop program that prompts the user to enter how many months they want to monitor their budget for, prompts for the amount they spent during the month, and their budget. It should tell the user how much they spent, their budget, and if they were over or under the budget.

this is what I have so far, I dont know where to go from here, and it is probably incorrect too.

print("""
This program will prompt you to enter your budget, and amount spent
for a certain month and calculate if your were under or over budget.
You will have the option of choosing how many months you would like to
monitor.n""")
AmountSpent = 0
Budget = 0
numMonths = float(input("Enter the number of months you would like to monitor:"))
while numMonths<0:
    print("nNegative value detected!")
    numMonths = float(input("Enter the number of months you would like to monitor"))
for month in [1,100]:
    print("n=====================================")
    AmountBudgeted = float(input("Enter amount budgeted for month "+month+":"))
    while AmountBudgeted<0:
         print("Negative value detected!")
         AmountBudgeted = float(input("Enter amount budgeted for month "+month+":"))
    if month == "1":
       print(f'your budget is {AmountBudgeted}.')
            
Asked By: Pillis

||

Answers:

To iterate from 1 to the number of months inclusive, you can use range(1,numMonths+1). The number of months should be an integer or else you will probably get an error from range(). You can get the amount under or over budget by subtracting one amount from the other. The following script has these changes and does the calculating and prints whether under or over budget.

print("""
This program will prompt you to enter your budget, and amount spent
for a certain month and calculate if your were under or over budget.
You will have the option of choosing how many months you would like to
monitor.n""")
AmountSpent = 0
Budget = 0
numMonths = int(input("Enter the number of months you would like to monitor:"))
while numMonths<0:
    print("nNegative value detected!")
    numMonths = int(input("Enter the number of months you would like to monitor"))
for month in range(1, numMonths+1):
    print("n=====================================")
    AmountBudgeted = float(input("Enter amount budgeted for month "+month+":"))
    while AmountBudgeted<0:
         print("Negative value detected!")
         AmountBudgeted = float(input("Enter amount budgeted for month "+month+":"))
    AmountSpent = float(input("Enter amount spent for month "+month+":"))
    while AmountSpent<0:
         print("Negative value detected!")
         AmountSpent = float(input("Enter amount spent for month "+month+":"))
    if AmountSpent <= AmountBudgeted:
        underBy = AmountBudgeted - AmountSpent
        print("Under budget by " + underBy)
    else:
        overBy = AmountSpent - AmountBudgeted
        print("Over budget by " + overBy)
    if month == "1":
       print(f'your budget is {AmountBudgeted}.')
Answered By: Nathan Mills
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.