Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

I checked all the spaces of the example and compare with my output i think i did it right. But i cant pass the last test (create a function to show a bar char). So, some suggests that i could do it wrong.

Your code so far

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

    
    def deposit(self, amount, description=''):
        
        deposit = {
            "amount":float(amount),
            "description": description
        }
        self.ledger.append(deposit)
        return True

    def withdraw(self, amount, description=''):
        if self.check_funds(float(amount)):
            withdraw = {
                "amount": -float(amount),
                "description": description
            }
            self.ledger.append(withdraw)
            return True
        else:
            return False
    
    def get_balance(self):
        self.balance = 0
        for amount in self.ledger:
            self.balance += amount['amount']
        return self.balance
    
    def transfer(self, amount, destination):
        if self.check_funds(float(amount)):
            self.withdraw(amount, f'Transfer to {destination.budget}')
            destination.deposit(amount, f'Transfer from {self.budget}')
            self.get_balance()
            return True
        
        else:
            return False
    
            
    def check_funds(self, amount):
        
        if self.get_balance() < amount:
            return False
        else:
            return True
    
    def __str__(self) -> str:
        self.get_balance()
        text =''
        text+=self.budget.center(30,"*")+'\n'
        ch_total = 30
        for trans in self.ledger:
            desc=trans['description']
            amount ='%.2f'%trans['amount']
            length = len(desc) + len(str(amount))
            
            if (len(desc) + len(str(amount)) )> 30:
                desc = trans['description'][:29-len(str(amount))]
                text += desc + " " +str(amount) + '\n'
                continue
            text += desc + " "*(ch_total-length)+ amount + '\n'
        text += "Total: " + '%.2f'%self.balance
        return text
    
def create_spend_chart(categories):
    count=0
    aux=0
    percentage = {}
    
    clasification, total_bills = cat_and_total_amount(categories)
    
    percentage = take_percentage(clasification, total_bills)
    
    string=draw_chart(percentage)
    
    if categories[-1]:
        string += "\n"+" "*4+'-'*(len(categories)*3+1)
    
    larger =  max(clasification.keys(), key=len)

    #print(larger)
    
    for lenght in range(len(larger)):
        string+='\n'
        for cat in clasification.keys():
            try:
                if cat == categories[0].budget:
                    if lenght == len(larger)-1:
                        
                        string += " "*6
                    else:
                        string += " "*5+cat[lenght]
                
                #elif larger[lenght] == larger[-1]:
               
                elif lenght == len(larger) and larger[lenght] == larger[-1]:
                    print(lenght, len(larger))
                    string += " "*2 + cat[lenght]
                 
                else:
                    string += " "*2+cat[lenght]
            except:
                
                string += " "*3
            
       # print(string)
    return string
                
def cat_and_total_amount(categories):
    clasification = {}
    total_bills = 0
    for category in categories:
        
        for bill in category.ledger:
            if bill['amount'] < 0 and bill['description'][:8].lower() != 'transfer':
                total_bills += bill['amount']
                if category.budget in clasification:
                    aux = clasification[category.budget] + bill['amount']
                    clasification[category.budget] = aux
                    
                else:
                    clasification[category.budget] = bill['amount']
    
    return clasification, total_bills
    
def take_percentage(clasification, total_bills):
    percentage = {}
    for category in clasification.keys():
        percentage[category] = round(clasification[category] / total_bills * 100,1)
    
    return percentage


def draw_chart(percentage):
    keys = percentage.keys()
    string=''
    
    try:
        string+='Percentage spent by category' + '\n'
        for col in range(100,-1,-10):
            if col >= 10 and col < 100:
                string += " " + str(col) + "|" + " "
            elif col < 10:
                string += "  " + str(col) + "|"+ " "
            else:
                string += "" + str(col) + "|"+ " "
            for key in keys:
                if percentage[key] >= col:
                    string +=  "o" + "  "
                else:
                    string +=  " "+ "  "
            if col != 0:
                string += '\n'
        return string
    except:
        pass
    

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)
clothing.deposit(800, "deposit")
clothing.withdraw(25, "pair of shoes")
clothing.withdraw(15, "hodies")
education = Category("Education")
education.deposit(500,"deposit")
education.withdraw(135, "tuition")
education.withdraw(90, "books")
health = Category('Health')
health.deposit(200, "deposit")
health.withdraw(100, "Medicine")
services = Category("Services")
services.deposit(100, "deposit")
services.withdraw(25, "light")

# print(food)
# print(clothing)
# print(education)
categories =[clothing,education,food,health]

print(create_spend_chart(categories))

###My output:

Screenshot 2024-05-18 235552

Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

If you take a look at the browser’s console, there will be details of the tests. Something strange happens with the category names on chart, when category has longer name - for example Entertainment.

Thank you! I could pass the tests :smiley:

1 Like

A post was split to a new topic: Build a Budget App Project - Build a Budget App