Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

The last check on this says the format isn’t exact. I’ve checked the verbose output in the console multiple times after messing with the spaces, but I cant work out where to put the extra spaces and/or why they’re not showing up.

Your code so far

class Category:
    balance=0
    def __init__(self,category):
        self.ledger = []
        self.entries=[]
        self.category=category
    def check_funds(self,amount):
        if amount<=self.balance:
            return True
        else:
            return False
    def deposit(self,amount,description=''):
        if not description:
            description=''
        self.ledger.append({"amount": amount, "description": description})
        self.balance+=amount
    def withdraw(self,amount,description=''):
        self.amount=-amount
        if not description:
            description=''
        if self.check_funds(amount):
            self.ledger.append({"amount": self.amount, "description": description})
            self.balance+=self.amount
            return True
        else:
            return False
    def total_expenses(self):
        money_out=0
        for entry in self.ledger:
            if entry["amount"]<0:
                money_out+=-1*entry["amount"]
        return money_out
    def transfer(self,amount,new_cat):
        if self.check_funds(amount):
            new_cat.deposit(amount,f'Transfer from {self.category}')
            self.withdraw(amount,f'Transfer to {new_cat.category}')
            return True
        else:
            return False
    def get_balance(self):
        return (round(self.balance,2))
    def __str__(self):
        string=self.category.center(30,'*')
        for entry in self.ledger:
            num="{:.2f}".format(entry["amount"])
            des=entry["description"]
            if len(des)>=(31-len(str(num))):
                des=des[0:29-len(str(num))]
            line=f'\n{des}{str(num).rjust(30-len(des))}'
            string+=line
        string+=f'\nTotal: {round(self.balance,2)}'

        return string

def create_spend_chart(categories):
    total_out=0
    percentage=0
    result=''
    lengths=[]
    verticals=[]
    chars=''
    start_index=0
    string='Percentage spent by category'+'\n'
    for category in categories:
        total_out+=category.total_expenses()
        lengths.append(len(category.category))
    max_length=max(lengths)
    print("total out:",total_out)
    for i in range(100,-1,-10):
        if len(str(i))>=3:
            index=(str(i)+'| ')
        elif i==0:
            index='  '+str(i)+'| '
        else:
            index=' '+str(i)+'| '
        string+=index
        for category in categories:
            percentage=round((category.total_expenses()/total_out)*100,0)
            if category==categories[0] and i<=percentage:
                                indicator='o '
            elif category==categories[0] and i>percentage:
                indicator='  '
            elif i>percentage:
                indicator='   '
            else:
                indicator=' o '
            string+=indicator
        string+='\n'
    string+='    '+'-'*3*len(categories)+'-'
    for category in categories:
        string_cat=category.category
        vertical=list(string_cat)
        if len(category.category)<max_length:
            vertical.extend(' ' for i in range(max_length-len(category.category)))
        verticals+=vertical
    for char in verticals:
        if start_index>=max_length:
            break
        chars+='\n'+'     '+verticals[start_index]+'  '+verticals[start_index+max_length]+'  '+verticals[start_index+2*max_length]
        start_index+=1
        if start_index>=max_length:
            break
    string+=chars
    return string

food = Category("Food")
food.deposit(100,"lots of money many character")
food.withdraw(10,"two")
clothing = Category("Clothing")
clothing.deposit(30,"thing")
food.transfer(0,clothing)
clothing.withdraw(30,"your mum")
entertainment=Category("Entertainment")
entertainment.deposit(960,"why not")
entertainment.withdraw(8,"huh")
print(food)
print(clothing)
print(create_spend_chart([clothing,food,entertainment]))


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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

Are you having trouble interpreting the error or trouble editing the code to correct it?

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.

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!

thanks, I was having trouble interpreting what the error message was so I’ll have a look at it again and see what I can come up with.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.