Build a Budget App - Build a Budget App

Tell us what’s happening:

I cannot get tests 20 and 24 to pass, and I am not sure what is going wrong. I have cross-referenced with people with the same issue, and can’t get it to work. Can someone please help me understand

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []

    def deposit(self, amount, description=""):
        self.ledger.append({"amount": amount, "description": description})

    def withdraw(self, amount, description=""):
        if self.check_funds(amount):
            self.ledger.append({"amount": -amount, "description": description})
            return True
        return False

    def get_balance(self):
        return sum(item["amount"] for item in self.ledger)

    def transfer(self, amount, category):
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {category.name}")
            category.deposit(amount, f"Transfer from {self.name}")
            return True
        return False

    def check_funds(self, amount):
        return self.get_balance() >= amount

    def __str__(self):
        title = f"{self.name:*^30}\n"
        body = ""
        for item in self.ledger:
            body += f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"
        return title + body + f"Total: {self.get_balance()}"


def create_spend_chart(categories):
    result = "Percentage spent by category\n"

    spent = []
    for category in categories:
        total = 0
        for item in category.ledger:
            if item["amount"] < 0:
                total += -item["amount"]
        spent.append(total)

    total_spent = sum(spent)
    percentages = [int((s / total_spent) * 100) // 10 * 10 for s in spent]

    for level in range(100, -1, -10):
        result += f"{level:>3}|"
        for p in percentages:
            result += " o " if p >= level else "   "
        result += "\n"

    result += "    " + "-" * (3 * len(categories) + 1) + "\n"

    max_len = max(len(cat.name) for cat in categories)
    for i in range(max_len):
        result += "     "
        for cat in categories:
            result += (cat.name[i] if i < len(cat.name) else " ") + "  "
        result += "\n"

    return result[:-1]

    
food = Category('Food')
food.deposit(1000, 'deposit')
print(food.get_balance())
print(food.check_funds(1000))
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)

clothing = Category('Clothing')
clothing.deposit(500, 'deposit')
clothing.withdraw(100, 'suit')

auto = Category('Auto')
auto.deposit(10000, 'deposit')
auto.withdraw(50, 'maintainance')

categories = [food, clothing, auto]
bar_chart = create_spend_chart(categories)
bar_chart = bar_chart.replace(' ', '*')
print(bar_chart)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Build a Budget App - Build a Budget App

Hi @edatema2009

You need to include spaces for each of the rows.

Happy coding

wait but I cant find where that line of code is ive been looking and im starting to think im just blind

Try placing an xat the end of one of the strings to help you find the part.

i found all of the parts but it wants me to add two spaces after the last bar on the graph the issue is once i do that test 19 fails

@Teller this is my updated code

class Category:

    def __init__(self, name):

self.name = name

self.ledger = []




def deposit(self, amount, description=""):

self.ledger.append({"amount": amount, "description": description})




def withdraw(self, amount, description=""):

if self.check_funds(amount):

self.ledger.append({"amount": -amount, "description": description})

return True

return False




def get_balance(self):

return sum(item["amount"] for item in self.ledger)




def transfer(self, amount, category):

if self.check_funds(amount):

self.withdraw(amount, f"Transfer to {category.name}")

            category.deposit(amount, f"Transfer from {self.name}")

return True

return False




def check_funds(self, amount):

return self.get_balance() >= amount




def __str__(self):

        title = f"{self.name:*^30}\n"

        body = ""

for item in self.ledger:

            body += f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"

return title + body + f"Total: {self.get_balance()}"





def create_spend_chart(categories):

    result = "Percentage spent by category\n"




    spent = []

for category in categories:

        total = 0

for item in category.ledger:

if item["amount"] < 0:

                total += -item["amount"]

        spent.append(total)




    total_spent = sum(spent)

    percentages = [int((s / total_spent) * 100) // 10 * 10 for s in spent]




for level in range(100, -1, -10):

        result += f"{level:>3}|"

for p in percentages:

            result += " o " if p >= level else "   "

        result += "\n"




    result += "    " + "-" * (3 * len(categories) + 1) + "\n"




    max_len = max(len(cat.name) for cat in categories)

for i in range(max_len):

        result += "     "

for cat in categories:

            result += (cat.name[i] if i < len(cat.name) else " ") + "  "

        result += "\n"




return result[:-1]





food = Category('Food')

food.deposit(1000, 'deposit')

print(food.get_balance())

print(food.check_funds(1000))

food.withdraw(10.15, 'groceries')

food.withdraw(15.89, 'restaurant and more food for dessert')

clothing = Category('Clothing')

food.transfer(50, clothing)




clothing = Category('Clothing')

clothing.deposit(500, 'deposit')

clothing.withdraw(100, 'suit')




auto = Category('Auto')

auto.deposit(10000, 'deposit')

auto.withdraw(50, 'maintainance')




categories = [food, clothing, auto]

bar_chart = create_spend_chart(categories)

bar_chart = bar_chart.replace(' ', '*')

print(bar_chart) 

however currently it is still throwing errors and I cannot find why. I think it is due to the fact that the code wants to have a second space after the dots but i only have one but as soon as I update that test 19 fails how would you go about solving this

Everything simply needs to be aligned.

If you highlight the example graph maybe you can see it more clearly?

Can you try sharing this again?

Something happened to the indentation?

class Category:

    def __init__(self, name):

self.name = name

self.ledger = []




def deposit(self, amount, description=""):

self.ledger.append({"amount": amount, "description": description})




def withdraw(self, amount, description=""):

if self.check_funds(amount):

self.ledger.append({"amount": -amount, "description": description})

return True

return False




def get_balance(self):

return sum(item["amount"] for item in self.ledger)




def transfer(self, amount, category):

if self.check_funds(amount):

self.withdraw(amount, f"Transfer to {category.name}")

            category.deposit(amount, f"Transfer from {self.name}")

return True

return False




def check_funds(self, amount):

return self.get_balance() >= amount




def __str__(self):

        title = f"{self.name:*^30}\n"

        body = ""

for item in self.ledger:

            body += f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"

return title + body + f"Total: {self.get_balance()}"





def create_spend_chart(categories):

    result = "Percentage spent by category\n"




    spent = []

for category in categories:

        total = 0

for item in category.ledger:

if item["amount"] < 0:

                total += -item["amount"]

        spent.append(total)




    total_spent = sum(spent)

    percentages = [int((s / total_spent) * 100) // 10 * 10 for s in spent]




for level in range(100, -1, -10):

        result += f"{level:>3}|"

for p in percentages:

            result += " o " if p >= level else "   "

        result += "\n"




    result += "    " + "-" * (3 * len(categories) + 1) + "\n"




    max_len = max(len(cat.name) for cat in categories)

for i in range(max_len):

        result += "     "

for cat in categories:

            result += (cat.name[i] if i < len(cat.name) else " ") + "  "

        result += "\n"




return result[:-1]





food = Category('Food')

food.deposit(1000, 'deposit')

print(food.get_balance())

print(food.check_funds(1000))

food.withdraw(10.15, 'groceries')

food.withdraw(15.89, 'restaurant and more food for dessert')

clothing = Category('Clothing')

food.transfer(50, clothing)




clothing = Category('Clothing')

clothing.deposit(500, 'deposit')

clothing.withdraw(100, 'suit')




auto = Category('Auto')

auto.deposit(10000, 'deposit')

auto.withdraw(50, 'maintainance')




categories = [food, clothing, auto]

bar_chart = create_spend_chart(categories)

bar_chart = bar_chart.replace(' ', '*')

print(bar_chart)

Is this what your indentation looks like?

Do you have an error in the console?

that is what my indentation looks like and there is no error

so if this is your indentation, what’s inside __init__?

wait yeah that is messed up idk why it keeps doing that the name and list are in it

that’s what @pkdvalis was referring to

if you can post your correct code then we can take a look

When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []

    def deposit(self, amount, description=""):
        self.ledger.append({"amount": amount, "description": description})

    def withdraw(self, amount, description=""):
        if self.check_funds(amount):
            self.ledger.append({"amount": -amount, "description": description})
            return True
        return False

    def get_balance(self):
        return sum(item["amount"] for item in self.ledger)

    def transfer(self, amount, category):
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {category.name}")
            category.deposit(amount, f"Transfer from {self.name}")
            return True
        return False

    def check_funds(self, amount):
        return self.get_balance() >= amount

    def __str__(self):
        title = f"{self.name:*^30}\n"
        body = ""
        for item in self.ledger:
            body += f"{item['description'][:23]:<23}{item['amount']:>7.2f}\n"
        return title + body + f"Total: {self.get_balance()}"


def create_spend_chart(categories):
    result = "Percentage spent by category\n"

    spent = []
    for category in categories:
        total = 0
        for item in category.ledger:
            if item["amount"] < 0:
                total += -item["amount"]
        spent.append(total)

    total_spent = sum(spent)
    percentages = [int((s / total_spent) * 100) // 10 * 10 for s in spent]

    for level in range(100, -1, -10):
        result += f"{level:>3}|"
        for p in percentages:
            result += " o " if p >= level else "   "
        result += "\n"

    result += "    " + "-" * (3 * len(categories) + 1) + "\n"

    max_len = max(len(cat.name) for cat in categories)
    for i in range(max_len):
        result += "     "
        for cat in categories:
            result += (cat.name[i] if i < len(cat.name) else " ") + "  "
        result += "\n"

    return result[:-1]


food = Category('Food')
food.deposit(1000, 'deposit')
print(food.get_balance())
print(food.check_funds(1000))
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)

clothing = Category('Clothing')
clothing.deposit(500, 'deposit')
clothing.withdraw(100, 'suit')

auto = Category('Auto')
auto.deposit(10000, 'deposit')
auto.withdraw(50, 'maintainance')

categories = [food, clothing, auto]
bar_chart = create_spend_chart(categories)
bar_chart = bar_chart.replace(' ', '*')
print(bar_chart)

this is my correct code

1 Like
Percentage*spent*by*category
100|*********
*90|*********
*80|*********
*70|*********
*60|*********
*50|*********
*40|****o****
*30|*o**o****
*20|*o**o**o*
*10|*o**o**o* |
**0|*o**o**o* |
****----------|
*****F**C**A**|
*****o**l**u**
*****o**o**t**
*****d**t**o**
********h*****
********i*****
********n*****
********g*****

Can you see how it’s not aligned at the right side?

Compare with

yes but no matter what I do the code refuses to fix that issue and as soon as i found a way to fix it the test 19 broke because it had 11 charachters per line

i found the solution thank you very much for your help

1 Like