Build a Budget App - Build a Budget App

Tell us what’s happening:

I’m stuck at 19. I tried to see what’s issue in the browser console, but it only says “Expected different rounding of bars”. I already tried different ways of rounding it, but nothing seems to work.

Your code so far

from itertools import zip_longest

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

    def deposit(self, amount, description=''):
        self.ledger.append(dict([('amount', amount),('description', description)]))

    def withdraw(self, amount, description=''):
        if self.check_funds(amount):
            self.ledger.append(dict([('amount', -amount),('description', description)]))
            return True
        return False

    def get_balance(self):
        balance = 0
        for transaction in self.ledger:
            balance += transaction.get('amount')
        return balance
        

    def transfer(self, amount, category):
        if self.check_funds(amount):
            self.withdraw(amount, f'Transfer to {category.name}')
            category.deposit(amount, f'Transfer from {self.name}')
            return True
        return False

    def check_funds(self, amount):
        if amount > self.get_balance():
            return False
        return True


    def __str__(self):
        header = f'{self.name:*^30s}'
        body = ''
        total = '\nTotal: ' + str(self.get_balance())
        for transaction in self.ledger:
            body += f"\n{transaction.get('description')[:23]:<23s}{'{:.2f}'.format(transaction.get('amount')):>7}"
        return header + body + total


def create_spend_chart(categories):
    tytle = 'Percentage spent by category'
    body = ''
    
    def balls(categories):
        balls_per_cat = {}
        total_spent = 1
        
        for category in categories:
            amount = 0
            
            for transaction in category.ledger:
                
                if transaction.get('amount') < 0:
                    amount += -transaction.get('amount')
                    total_spent += -transaction.get('amount')
            
            balls_per_cat[category.name] = amount
        
        for category in balls_per_cat:
            balls = round(balls_per_cat[category] / total_spent * 10)
            balls_per_cat[category] = f"o{'o'*balls:<10}"
        
        return balls_per_cat


    balls_per_cat = balls(categories)
    

    for i in range(100,-1,-10):
        body += f"\n{i:>3}|"
        for ball in balls_per_cat:
            body += f" {balls_per_cat[ball][-1:]} "
            balls_per_cat[ball] = balls_per_cat[ball][:-1]
        body += ' '
    line = f"\n    {len(balls_per_cat)*'---'}-"

    names_ = []
    for name in categories:
        names_.append(name.name)
    
    names = ''
    for letter in zip_longest(*names_, fillvalue=' '):
        names += f"\n     {'  '.join(letter)}  "
    
    
    
    
    
    chart = tytle + body + line + names
    return chart


food = Category('Food')
food.deposit(1000, 'deposit')
food.withdraw(10.15, )
food.withdraw(15.89, 'restaurant and more food for dessert')
clothing = Category('Clothing')
food.transfer(50, clothing)
transport = Category('Transport')
print(food)
print(clothing)
print(transport)
print(create_spend_chart([food, clothing]))



Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0

Challenge Information:

Build a Budget App - Build a Budget App

this is useful:

it looks like there is a rounding error on one of the bars

why does total spent starts at 1?

total_spent = 1

are you rounding correctly?
maybe review user story 5

    balls_per_cat[category.name] = amount
        
        for category in balls_per_cat:
            balls = balls_per_cat[category] / total_spent * 100
            print(balls)
            balls = int(round(balls, -1) / 10)
            print(balls)
            balls_per_cat[category] = f"o{'o'*balls:<10}"
        
        return balls_per_cat

I realy tried but is always the same error

how does int round? does it round down?

  • Calculate percentages from withdrawals only and not from deposits. The percentage should be the percentage of the amount spent for each category to the total spent for all categories (rounded down to the nearest 10).

it says “rounded down to the nearest 10”, so you need methods that round down