Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I am not getting the expected output from the bar chart function.

Your code so far

class Category:
    def __init__(self,category_name):
        self.category_name=category_name
        self.ledger=[]
    def __str__(self):
        string=''
        one_side=(30-len(self.category_name))//2
        string+='*'*one_side+self.category_name+'*'*one_side+'\n'
        for item in self.ledger:
            desc_length=len(item['description'])
            if desc_length<23:
                string+=item['description']+' '*(23-desc_length)
            else:
                desc_cut=item['description'][:23]
                string+=desc_cut
            amount_len=len(f"{(float(item['amount'])):.2f}")
            if amount_len<=7:
                string+=' '*(7-amount_len)+f"{(float(item['amount'])):.2f}"+'\n'
            else:
                while(amount_len>7):
                    cash=list(f"{(float(item['amount'])):.2f}")
                    cash.pop()
                    amount_len-=1
                string+=''.join(cash)+'\n'
        string+= "Total: "
        string += f"{self.get_balance():.2f}"
        return string
    def check_funds(self,amount):
        return amount <= sum([item['amount'] for item in 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
        else:
            return False
    def get_balance(self):
        return sum([item['amount'] for item in self.ledger])
    def transfer(self,amount,another_category):
        if self.check_funds(amount):
            self.ledger.append({'amount':-amount,f'description':f'Transfer to {another_category.category_name}'})
            another_category.deposit(amount,f'Transfer from {self.category_name}')
            return True
        else:
            return False

def create_spend_chart(categories):
    bar_chart='Percentage spent by category\n'
    num_categories=len(categories)
    total=[]
    spent=[]
    percent=[]
    names=[]
    for i in range(num_categories):
        temp=categories[i]
        names.append(categories[i].category_name)
        total.append(sum([item['amount'] if item['amount']>0 else 0 for item in temp.ledger]))
        spent.append(total[i]-temp.get_balance())

        try:
            percent.append(10 * round((spent[i]*100/total[i])/10))
        except:
            percent.append(0)
    for i in range(10,-1,-1):
        if i==10:
            bar_chart+=f'{i*10}| '
        elif i==0:
            bar_chart+='  0| '
        else:
            bar_chart+=f' {i*10}| '
        for j in range(num_categories):
            if percent[j]<i*10:
                bar_chart+='   '
            else:
                bar_chart+='o  '
        bar_chart+=f'\n'
    bar_chart+='    '+'-'*num_categories*3+'\n'
    biggest_name_length=max([len(name) for name in names])
    for i in range(biggest_name_length):
        bar_chart+='    '
        for j in range(num_categories):
            try:
                bar_chart+=f' {names[j][i]} '
            except:
                bar_chart+=f'   '
        if i==biggest_name_length-1:
            bar_chart+=''
        else:
            bar_chart+=' \n'
    return bar_chart
Clothing=Category('Clothing')
food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, 'groceries')
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
print(food)
categories=[food,Clothing]
print(create_spend_chart(categories))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0

Challenge Information:

Build a Budget App Project - Build a Budget App Project

output is

console
image

Seems like you are not iterating through all of the categories? Look at this line.

Use print() to print out your variables and make sure they are working as expected.

it does work, if it didn’t work shouldn’t it not print the clothing class

Check the browser console (press F12) for a more verbose error output to see the expected spacing.

An assertion error gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

thank you I found the problem, the way I am rounding of isn’t working well

is there a way for me to get the input?

I am rounding off like this


could you give me what is wrong or give the input for which this went wrong?
console output is

Many lines are off, so it doesn’t seem like a rounding error, which would only be off by 10.

Are you calculating the amount spent in a category as a percent of the total spent across all categories? (not total deposit)

I was calculating how much was spent from each category and displaying it.
Now though, there’s again errors


why is there two ? for the same error? and if + denotes the error, shouldn’t be there only one?
and the code has been changed to

is the withdrawal from one account during transfer not actually considered?

Transfers do count as withdrawls, I believe. Now it looks like a rounding error.

The height of each bar should be rounded down to the nearest 10.

Last 5 lines are my rounding of code.
First get the spending in a category out of total spending into percentage
Second divide by 10 so that rounding of gives integer less than 10
Lastly multiply by 10

Can you print it out?

What percentages are you getting before and after rounding?

image
on my input
is there a way to get the failed test input?
there are a few cases before this that works as intended

found the problem, in the example its not using round, but floor.

1 Like

Exactly, you need to round down, not to the closest 10:

The height of each bar should be rounded down to the nearest 10.

79.77 should round down to 70.