Loop and sum in python with user input

Viewed 581

I'm trying to calculate the budget by asking the user to enter the amount they want to budget and break it down by specific amount and substract that amount from the original with every iteration and show them the last result

budget = int(input("Please enter the amount you have budgeted for this month: "))

print(budget)

expensess = ['Rent','Food','Car','Gym','Phone','Travel','Savings']


balance=0
budget = budget
for i in expensess:

    added_balance = int(input('How much did you budget for '+str(i)))
    new_balance = int(budget - added_balance)
    print(new_balance)
    balance += new_balance
    budget = balance
    print("budget is "+str(budget))
    

if balance is > budget:
    print("You underbudgeted ")
else:
    print('Good Job you would have extra money to spend'+ )    

when I run this

Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
=== RESTART: C:\Users\boxfo\Desktop\Programming Challenges\Budget Analysis.py ==
Please enter the amount you have bugeted for this month: 6000
6000
How much did you budget for Rent2000
budget is 4000
How much did you budget for Food2000
budget is 6000
How much did you budget for Car3000
budget is 9000
How much did you budget for Gym
2 Answers

I've changed a bit your code. Just have a look:

budget = int(input("Please enter the amount you have budgeted for this month: "))

print("Total budget %d " % (budget) + "\n")

expenses_list = ["Rent", "Food", "Car", "Gym", "Phone", "Travel", "Savings"]

expenses = 0

for i in expenses_list:

added_balance = int(input("How much did you budget for " + str(i) + "\t"))

# CHECK IF THE ADDED BUDGET IS GREATER THAN THE TOTAL BUDGET (budget)
if added_balance > budget:
    print("Your expense cannot be higher than your total budget!")
    exit()  # TERMINATES THE PROGRAMM OR ASK ANOTHER VALUE IF YOU WANT

expenses += added_balance

# NOW CHECK IF YOUR EXPENSES ARE ALREADY OVER THE BUDGET
if (budget - expenses) < 0:
    print(
        "YOUR EXPENSES HAVE REACHED %d" % (expenses)
        + " AND YOUR BUDGET IS %d" % (budget)
    )
    exit()  # TERMINATES THE PROGRAMM OR ASK ANOTHER VALUE IF YOU WANT

print("ADDED BUDGET: %d" % (added_balance))
print(
    "CURRENT BALANCE IS: %d " % (budget - expenses)
    + "\n ------------------------ \n"
)

You should decrease the budget variable and you can calculate everything what you want in your question:

budget -= balance

I have written a working version from your code with some comments.

Code:

budget = int(input("Please enter the amount you have budgeted for this month: "))

expensess = ["Rent", "Food", "Car"]

for i in expensess:
    added_balance = int(input("How much did you budget for {}: ".format(i)))
    budget -= added_balance  # decrease the budget variable with the spend money
    print("Current budget is {}".format(budget))

if 0 > budget:  # If the budget is a minus number then you are underbudgeted.
    print("You underbudgeted: {}".format(abs(budget)))  # Creating ABS from negative number and print the difference.
else:
    print("Good Job you would have extra money to spend: {}".format(budget))

Tests:

Underbudgeted:

>>> python3 test.py
Please enter the amount you have budgeted for this month: 6000
How much did you budget for Rent: 2000
Current budget is 4000
How much did you budget for Food: 2000
Current budget is 2000
How much did you budget for Car: 3000
Current budget is -1000
You underbudgeted: 1000

Good job:

>>> python3 test.py
Please enter the amount you have budgeted for this month: 6000
How much did you budget for Rent: 2000
Current budget is 4000
How much did you budget for Food: 1000
Current budget is 3000
How much did you budget for Car: 1000
Current budget is 2000
Good Job you would have extra money to spend: 2000
Related