Build a Budget App - Build a Budget App

Tell us what’s happening:

Still can not pass . Should it use alignment of the categories?

Your code so far

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

    def __str__(self):
        title = f"{self.name:*^30}\n"
        display=''
        for entry in self.ledger:
            desc = entry["description"][:23]         
            amount = entry["amount"]
            display=display+f"{desc:<23}{amount:>7.2f}\n"
 # Total line
        total = self.get_balance()
        balance=f"Total: {total:.2f}"
        return title+display+balance.rstrip()

    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 * -1, 'description': description})
            return True
        return False
    
    def get_balance(self):
        return sum(entry["amount"] for entry in self.ledger)
    
    def transfer(self,amount,category):
        if self.check_funds(amount):
            source=self
            destination= category 
            source.withdraw(amount,f'Transfer to {destination.name}')
            destination.deposit(amount,f'Transfer from {source.name}')
            return True
        return False
    
    def check_funds(self, amount):
        if amount>self.get_balance():
            return False 
        return True

def view_chart_categ(categories):
    cat_percentages = categories

#Create the chart category part 
    keys = list(cat_percentages.keys())
    max_len = max(len(key) for key in keys)
    categ=''
    for i in range(max_len):
        categ=categ+'    '
        for key in keys:
            categ=categ+' '
            if i < len(key):
                categ=categ+key[i]+' '
            else:
                categ=categ+'  '
        categ=categ+' \n'
    return categ.rstrip()

def view_bars(categories):

    #Convert all float to in
    int_data = {k: int(v) for k, v in categories.items()}
    #Create a bar for each category
    bar='o' 
    space=' '
    bars=[]
    for key,value in int_data.items():
        value=round(value,-1) / 10
        bars.append(bar*int(value+1)+space*(10-int(value)))
                
    #Display chart with the bars 
    chart=''
    for i in range(100,-1, -10):
        o=int(i/10)
        barchart=''
        for bar in bars:
            barchart+=bar[o]+'  '
        chart+=f'{i:3}| {barchart}\n'
    return chart.rstrip()

def create_spend_chart(categories): 
    cat_withdraw={}
    for cat in categories:
        cat_withdraw[cat.name]=sum(entry["amount"] for entry in cat.ledger if entry["amount"] < 0) * -1

    total_withdrawals=int(sum(cat_withdraw.values()))

    #'Percentage spent by category'
    total_percentages = dict(map(lambda item: (item[0], (item[1] / total_withdrawals) * 100), cat_withdraw.items()))
    #Print the whole chart 
    print('Percentage spent by category')
    print(view_bars(total_percentages))
    #Print Horizontal line
    print(f'    {"-"*10}')
    print(view_chart_categ(cat_withdraw))
    
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)
auto = Category("Auto")
auto.deposit(1000, "deposit")
auto.withdraw(50.00, "fuel")
food.transfer(200, clothing)
clothing.withdraw(100.00, "socks")

categories =[food,clothing,auto]
for categ in categories:
    print(categ)

create_spend_chart(categories)

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Mobile Safari/537.36

Challenge Information:

Build a Budget App - Build a Budget App

Welcome to the forum @jcmaristela

Your code is printing the title message, try returning it.

Happy coding

19. The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.

23. create_spend_chart chart should have each category name written vertically below the bar. Each line should have the same length, each category should be separated by two spaces, with additional two spaces

24. create_spend_chart should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details.

This are the problem . I used round(value,-1). I wonder why it’s still not pass.

I see nearest down 10 not nearest 10

Only 23 and 24 issues I can’t pass

if you have made updates to your code and still need help, please share your updated code

I solved all issues already. Only the last part of the chart has a problem ang I managed to solved it. Thanks for the help. ‘\n’

1 Like