Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I am out of ideas here. I am still failing the last two test but everything looks fine to me both when a print a category and print the chart as show with the examples in my code. What am I doing wrong?

Your code so far

class Category:
    def __init__(self,category):
        self.category = category
        self.budget = 0
        self.ledger = []

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

    def withdraw(self,amount,description=''):
        if not self.check_funds(amount):
            return False
        else:
            self.budget -= amount
            self.ledger.append({'amount':-amount,'description':description})
            return True
    def get_balance(self):
        return self.budget
    
    def transfer(self,amount,another_budget):
        if self.check_funds(amount):
            self.withdraw(amount,f'Transfer to {another_budget.category}')
            another_budget.deposit(amount,f'Transfer from {self.category}')
            return True
        return False
    
    def check_funds(self,amount):
        if amount > self.budget:
            return False
        return True

    def __str__(self):
        output = ''
        title = self.category.center(30,'*')
        output += f'{title}\n'
        for item in self.ledger:
            description = item["description"]
            amount = item["amount"]
            output += f"{description[:23]:23} {amount:7.2f}\n"
        output += f'Total: {self.budget:.2f}'
        return output
        

food = Category('Food')
food.deposit(1000,'A grant')
tfee = Category('tfee')
auto = Category('Auto')

food.transfer(560,tfee)
food.withdraw(345,'For baby items and other miscellanous items')
tfee.withdraw(440,'for ewa')
tfee.transfer(40,auto)
food.transfer(70,auto)
auto.withdraw(90,'For Tires')
print(auto)
def create_spend_chart(data):
    categories = [cat.category for cat in data]
    mini_per = []
    sum = 0
    for i in data:
        for item in i.ledger:
            if item['amount']<0:
                sum += -1*(item['amount'])
        mini_per.append(sum)
        sum = 0
    
    total = 0
    for tot in mini_per:
        total += tot
    percentages = [round((i/total)*100) for i in mini_per]

    chart = 'Percentage spent by category\n'
    for num in range(100,-10,-10):
        chart += f"{str(num) + '|':>4}"
        for percent in percentages:
            if percent >= num:
                chart += 'o  '
            else:
                chart += '  '
        chart += ' \n'


    chart += '    '+'-'*10+'\n'

    height = len(max(categories,key=len))
    padded = [name.ljust(height) for name in categories]
    for names in zip(*padded):
        chart += f"    {'  '.join(names)}\n"

    print(chart)
create_spend_chart([food,tfee,auto])

Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

You should return chart instead of print it. Change that and then open the console with F12 to see the difference between your output and the expected one.

I have changed that and the console is looking more confusing than the test itself. Maybe spacing is causing the error in the last test what of the second to the last?

It might seem confusing but all the information you need is there. For example:
image
a - indicates the incorrect output
a + shows what it should be
and the question mark line indicates the place in which there’s a difference between the actual and the expected output. The +++ indicate three missing spaces.

From what I am observing on the second to last test it seems to be excess line and I tried removing the new line character and the output was squeezed and not helpful. Can you run it and tell me the issue is?

I am still stuck :cry:
The error is confusing

For the second to last test, lines are one character longer than they should. Look at that part to find out why.

Second to the last corrected. I also tweaking the last to see if it works but I am still missing the tiny detail. Finding out the spacing error is has been difficult. I need a hand on that.
Thanks.


You need to go line by line and see the difference. The first line that contains a difference is where you have 100| followed by spaces. You need three more spaces (corresponding to +++).
Then, you are not spacing the os as in the example. The first column of the chart should be spaced from the axis, as well as the name of the category.

An assertion error gives you a lot of information to track down a problem. For 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.

I think the problem here is English - space this space that ’ is messing with head to be honest. I think I might take a break maybe I will understand it in the future:

def create_spend_chart(data):
    categories = [cat.category for cat in data]
    mini_per = []
    sum = 0
    for i in data:
        for item in i.ledger:
            if item['amount']<0:
                sum += -1*(item['amount'])
        mini_per.append(sum)
        sum = 0
    
    total = 0
    for tot in mini_per:
        total += tot
    percentages = [round((i/total)*100) for i in mini_per]
#The issue should be from here....
    chart = 'Percentage spent by category\n'
    for num in range(100,-10,-10):
        chart += f"{str(num) + '|':>4} "
        for percent in percentages:
            if percent >= num:
                chart += 'o  '
            else:
                chart += '   '
        chart += ' \n'


    chart += '    '+'-'*10+'\n'

    height = len(max(categories,key=len))
    padded = [name.ljust(height) for name in categories]
    for names in zip(*padded):
        chart += f"     {'  '.join(names)}\n"
#....

    return chart

Here is your error:

AssertionError: 
'Perc[32 chars]     \n 90|       \n 80|       \n 70|  o     \[284 chars] t\n' != 
'Perc[32 chars]        \n 90|          \n 80|          \n 70|[342 chars] t  '

Your output is first, you can more clearly see the differences this way. For example your output ends with a newline, but the expected output does not.

If I remove the new line character then my '100| goes to the same line as the title

I have corrected the title part. From the console their is a repeating new line after every line and I not seeing it in the code. Any new line character I removes results to the code squeezing.

def create_spend_chart(data):
    categories = [cat.category for cat in data]
    mini_per = []
    sum = 0
    for i in data:
        for item in i.ledger:
            if item['amount']<0:
                sum += -1*(item['amount'])
        mini_per.append(sum)
        sum = 0
    
    total = 0
    for tot in mini_per:
        total += tot
    percentages = [round((i/total)*100) for i in mini_per]
    title = 'Percentage spent by category'
    chart = ''
    for num in range(100,-10,-10):
        chart += f"{str(num) + '|':>4} "
        for percent in percentages:
            if percent >= num:
                chart += 'o  '
            else:
                chart += '   '
        chart += '\n'


    chart += '    '+'-'*10+'\n'

    height = len(max(categories,key=len))
    padded = [name.ljust(height) for name in categories]
    for names in zip(*padded):
        chart += f"     {'  '.join(names)}\n"

    return f'{title}\n{chart}'

You need to remove the newline character only for the last line. Add whatever logic, if statements you need.

You need to change your code so that the output is exactly the same as the example.