Build a Budget App - Build a Budget App

Tell us what’s happening:

Hello, I am stuck on step 18 and my console does not print it right. It does not print the 'o’s and the names for the categories are not under the chart but in the left side. Can someone see what I have done wrong?

Your code so far

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

#Tilføj transaction til listen

    def deposit(self, amount, description=''):
        self.balance += amount
        self.ledger.append({'amount': amount, 'description': description})

#Træk fra kontoen

    def withdraw(self, amount, description=''):
        if self.check_funds(amount):
            self.deposit(-amount, description)
            return True
        else:
            return False

    def get_balance(self):
        return self.balance

    def transfer(self, amount, another_category):
        if self.check_funds(amount):
            self.balance -= amount
            self.ledger.append({'amount': -amount, 'description': f'Transfer to {another_category.name}'})
            another_category.deposit(amount, f'Transfer from {self.name}')
            return True
        else:
            return False

    def check_funds(self, amount):
        if amount <= self.balance:
            return True
        else:
            return False

    def __str__(self):
        output = self.name.center(30,'*') + '\n'
        for item in self.ledger:
            amount = item['amount']
            description = item['description']
            output += f'{description[0:23]:23}{amount:7.2f}'
            output += '\n'
        
        output += 'Total: '
        output += format(self.balance, '.2f')

        return output
        


def create_spend_chart(categories):
    categories_name = []
    categories_procent = []
    chartstr = 'Percentage spent by category\n'

#beregning af procentdele
    total_withdrawal = 0

    for category in categories:
        procent = 0
        categories_name.append(category.name)
        for item in category.ledger:
            amount = item['amount']
            if amount < 0:
                total_withdrawal += -amount
                procent += (round(-amount/total_withdrawal/10))*1000
                categories_procent.append(procent)
            else:
                None
                

#Lav grafen
    for num in range(100, 0, -10):
        chartstr += f"{str(num) + '|':>4}"
        for procent in categories_procent:
            if procent >= num:
                chartstr += 'o  '
            else:
                chartstr += '  '
        chartstr += '\n'
    chartstr += '    ' + (3*len(categories)+2)*'-' + '\n'

#Indsæt kategorinavne
    max_length = max(len(category) for category in categories_name)
    for i in range(0, max_length):
        lodret_navne = '     '
        for category in categories_name:
            if i < len(category):
                lodret_navne += category[i] + ' '
            else:
                lodret_navne += ' '
            lodret_navne += '\n'
        
        chartstr += lodret_navne
    return chartstr


food = Category('Food')
food.deposit(1000, 'Initial deposit')
food.withdraw(10.15, 'Groceries')
food.withdraw(15.89, 'Restaurant')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.withdraw(30,'Trousers')
print(food)
print("\n")
print(clothing)
print("\n")
print(create_spend_chart([food,clothing]))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15

Challenge Information:

Build a Budget App - Build a Budget App

That makes it look like there’s new line character added after each letter in the category name. Which part of code is responsible for the category names?

Okay so now I moved the new line character one indentation out and now the console looks like this, but I still can’t figure out why it won’t print the ‘o’s

Looking at the:

    for num in range(100, 0, -10):
        chartstr += f"{str(num) + '|':>4}"
        for procent in categories_procent:
            if procent >= num:
                chartstr += 'o  '
            else:
                chartstr += '  '
        chartstr += '\n'
    chartstr += '    ' + (3*len(categories)+2)*'-' + '\n'

What does it mean that no os are printed from this code - on which values that depends on? Following this further, what exactly are these values? Are they correct?

It must mean that i calculated the wrong percentages before so its not in the right range, but i am not sure i completely understand :frowning:

Now I changed the categories_procent(percentages of the different categories) to a dictionary(with name of category and percent) and then used the values(percent) to make the ‘o’s

Does it work now? If not, please share your updated code (as text), to make helping possible.

No it still does not print the percentages as ‘o’s, but the names are formatted right now, thanks :slight_smile:

Link to code

that is the link to the challenge, the link does not contain your code

Okay these are the lines I changed:

def create_spend_chart(categories):

categories_name = \[\]

categories_procent = {}

chartstr = 'Percentage spent by category\\n'

#beregning af procentdele

total_withdrawal = 0

for category in categories:

    procent = 0

    categories_name.append(category.name)

for item in category.ledger:

        amount = item\['amount'\]

if amount < 0:

            total_withdrawal += -amount

            procent += (round((-1\*amount)/total_withdrawal)\*10)\*10

            categories_procent.update({'{category.name}' : procent})

else:

None

#Lav grafen

for num in range(100, 0, -10):

    chartstr += f"{str(num) + '|':>4}"

for value in categories_procent:

if procent >= num:

            chartstr += ' o '

else:

            chartstr += '   '

    chartstr += '\\n'

chartstr += '    ' + (3\*len(categories)+1)\*'-' + '\\n'

#Indsæt kategorinavne

max_length = max(len(category) for category in categories_name)

for i in range(0, max_length):

    lodret_navne = '     '

for category in categories_name:

if i < len(category):

            lodret_navne += category\[i\] + '  '

else:

            lodret_navne += '   '

    lodret_navne += '\\n'

    chartstr += lodret_navne

return chartstr

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 (').

Thank you so much, this is the part were i can’t figure out why it won’t print the ‘o’s:

def create_spend_chart(categories):
    categories_name = []
    categories_procent = []
    chartstr = 'Percentage spent by category\n'

#beregning af procentdele
    total_withdrawal = 0

    for category in categories:
        procent = 0
        categories_name.append(category.name)
        for item in category.ledger:
            amount = item['amount']
            if amount < 0:
                total_withdrawal += -amount
                procent += (round(-amount/total_withdrawal/10))*1000
                categories_procent.append(procent)
            else:
                None
                

#Lav grafen
    for num in range(100, -10, -10):
        chartstr += f"{str(num) + '|':>4}"
        for procent in categories_procent:
            if procent >= num:
                chartstr += 'o  '
            else:
                chartstr += '  '
        chartstr += '\n'
    chartstr += '    ' + (3*len(categories)+1)*'-' + '\n'

#Indsæt kategorinavne
    max_length = max(len(category) for category in categories_name)
    for i in range(0, max_length):
        lodret_navne = '     '
        for category in categories_name:
            if i < len(category):
                lodret_navne += category[i] + '  '
            else:
                lodret_navne += '   '
        lodret_navne += '\n'
        
        chartstr += lodret_navne
    return chartstr


food = Category('Food')
food.deposit(1000, 'Initial deposit')
food.withdraw(10.15, 'Groceries')
food.withdraw(15.89, 'Restaurant')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.withdraw(30,'Trousers')
auto = Category('Auto')
auto.deposit(400, 'Initial deposit')
auto.withdraw(200, 'New wheels')

print(food)
print("\n")
print(clothing)
print("\n")
print(create_spend_chart([food, clothing, auto]))

I would check your procent and categories_procent, put some prints there, do they have the values you expect?

Hi, I tried printing the list of the percentages, the names of the categories and the total withdrawal now, it seems like the problem is with the calculation of the percentages but I can’t figure out where I made a mistake. But I can’t understand why it has five different things in the percentages list when I only have 3 categories

print all the things here until you find where the issue is

1 Like

I have come to the conclusion that the problem is that every time a withdrawal is found in a category the percentage is added to the list, but if there are multiple withdrawals within one category it does not sum those percentages but just adds it as another percentage in the list. How do I fix that?

consider if you should calculate the percentage for each single withdrawal, or if you maybe want to first calculate how much is withdrawn in each category, and then calculate the percentage

you are currently doing this for the total withdrawal of all categories, aren’t you?

build on that

Thank you so much for the help, I think I did it by dividing the calculation of the amount spent by category and the percentage of the withdrawals!