Anthony Barnhart
Anthony Barnhart

Reputation: 13

Basic checking account balance

I am trying to write a program that asks for a starting balance, expenses to subtract. If the user selects yes for more expenses it loops correctly and asks for another entry. If user selects N the program stops and prints Starting balance, Total number of expense entries and the ending balance after all expenses. The following code outputs it all correctly except it doesn't output the correct amount after all expenses.

# Initialize the accumulator.
account_balance = 0
ending_balance = 0
expense = 0
keep_going = "y"

# Prompt user for account balance.
account_balance = float (input("Enter the starting balance of the account you want to use:"))

# Calculate the balance.
for a in range (1000):    
    while keep_going == "y":

        # Prompt user for an expense.
        purchase = int (input("What is the value of the expense?:"))
        
        # Calculate the remaining balance.
        ending_balance = account_balance - purchase

        #Calculate the total expenses entered.
        expense = expense + 1

        # Ask if user has another expense.
        keep_going = input("Do you have another expense? (Enter y for yes)")                       

if keep_going == "n":
    print("Amount in account before expense subtraction $", format (account_balance, ",.2f"))
    print("Number of expenses entered:", expense)

    print("Amount in account AFTER expenses subtracted is $", format (ending_balance, ",.2f"))

# Pseudo Code

# Prompt user to enter amount in account in which money will be withdrawn from
# Prompt user to enter amount of first expense
# Subtract expense from account
# Ask user if they would like to add another expense
    # If "yes" then ask user to add expense
    # If "no" then display the following
        # Amount in account BEFORE expenses
        # Number of transactions made
        # Amount in account AFTER all expenses are subtracted

Upvotes: 1

Views: 1307

Answers (1)

Derek O
Derek O

Reputation: 19600

If you want the account balance to be the starting balance, then it needs to remain the starting balance throughout, and the ending balance should be the one updating.

# Initialize the accumulator.
account_balance = 0
ending_balance = 0
expense = 0
keep_going = "y"

# Prompt user for account balance.
account_balance = float (input("Enter the starting balance of the account you want to use:"))
# set ending balance to be the starting balance at the beginning
ending_balance = account_balance

# Calculate the balance.
for a in range (1000):    
    while keep_going == "y":

        # Prompt user for an expense.
        purchase = int (input("What is the value of the expense?:"))

        # Update the ending balance instead
        ending_balance = ending_balance - purchase

        #Calculate the total expenses entered.
        expense = expense + 1

        # Ask if user has another expense.
        keep_going = input("Do you have another expense? (Enter y for yes)")                       

if keep_going == "n":
    print("Amount in account before expense subtraction $", format (account_balance, ",.2f"))
    print("Number of expense entered:", expense)
    print("Amount in account AFTER expenses subracted is $", format (ending_balance, ",.2f"))

Upvotes: 1

Related Questions