Scientific Computing with Python Projects - Budget App

Tell us what’s happening:

Hello

I have completed the code. For all I know it is working as intended but I am not passing the last test. The instructions are to account for withdrawals but don’t mention transfers, I am taking those into account as well.

I have seen some post of people testing their code with replit but I don’t know how to do that.

Thanks

Your code so far

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

    def __str__(self):
        output = f'{self.cat:*^30}\n'
        for item in self.ledger:
            output += f'{item["description"][:23]:<23}{float(item["amount"]):>7.2f}\n'
        output += 'Total: ' + str(self.balance)
        return output

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

    def withdraw(self, amount, description = ''):
        if self.check_funds(amount):
            self.balance -= amount
            self.ledger.append({'amount':-amount, 'description':description}
            )
            return True
        else:
            return False

    def get_balance(self):
        return self.balance

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

    def check_funds(self, amount):
        if self.balance >= amount :
            return True
        else:
            return False


def create_spend_chart(categories):
    n = len(categories)
    total_expenses = 0
    expenses = []
    def spent(category):
        spent = 0
        for item in category.ledger:
            if float(item["amount"]) < 0:
                spent -= float(item["amount"])
        return spent
    # Spent por categoría
    for category in categories:
        expenses.append(spent(category))
        total_expenses += spent(category)

    expenses_tuple = list(zip([category.cat for category in categories], [int(100*spent(category)/total_expenses // 10) for category in categories]))
    # Create string
    percentages = f'Percentage spent by category\n'
    for i in range(10):
        percentages += f'{str(100-10*i):>3}|'
        line = ''
        for category in expenses_tuple:
            if category[1] >= 10 - i: 
                line += ' o '
            else:
                line += '   '
        percentages += line + ' \n'
    
    percentages += 4*' ' + (3*n+1)*'-' + '\n'
    l = max([len(item[0]) for item in expenses_tuple])
    for i in range(l):
        percentages += 5*' '
        for category in expenses_tuple:
            if i < len(category[0]):
                percentages += category[0][i] + 2*' '
            else:
                percentages += 3*' '
        percentages += '\n' if i + 1 < l else ''
    return(percentages)
   

Your browser information:

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

Challenge Information:

Scientific Computing with Python Projects - Budget App

I’ve added some test code to see what chart will be printed.

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")
clothing.deposit(100, "deposit")
clothing.withdraw(50, "payment")
food.transfer(50, clothing)

print(create_spend_chart([food, clothing]))
Percentage spent by category
100|       
 90|       
 80|       
 70|       
 60| o     
 50| o     
 40| o     
 30| o  o  
 20| o  o  
 10| o  o  
    -------
     F  C  
     o  l  
     o  o  
     d  t  
        h  
        i  
        n  
        g  

Take a look at the percentage labels - 0 is missing.

Oh my do I feel stupid. Thank you for that.

Fixed it, but it still fails to pass. Also removed an extra line at the end.

class Category:
    def __init__(self, name):
        self.cat = name
        self.ledger = []
        self.balance = 0.0
        self.spent = 0.0

    def __str__(self):
        output = f'{self.cat:*^30}\n'
        for item in self.ledger:
            output += f'{item["description"][:23]:<23}{float(item["amount"]):>7.2f}\n'
        output += 'Total: ' + str(self.balance)
        return output

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

    def withdraw(self, amount, description = ''):
        if self.check_funds(amount):
            self.balance -= amount
            self.spent += amount
            self.ledger.append({'amount':-amount, 'description':description}
            )
            return True
        else:
            return False

    def get_balance(self):
        return self.balance

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

    def check_funds(self, amount):
        if self.balance >= amount :
            return True
        else:
            return False


def create_spend_chart(categories):
    n = len(categories)
    total_expenses = 0
    expenses = []
    # Spent por categoría
    for category in categories:
        expenses.append(category.spent)
        total_expenses += category.spent

    expenses_tuple = list(zip([category.cat for category in categories], [int(100*category.spent/total_expenses // 10) for category in categories]))
    # Create string
    percentages = f'Percentage spent by category\n'
    for i in range(11):
        percentages += f'{str(100-10*i):>3}|'
        for category in expenses_tuple:
            if category[1] >= 10 - i: 
                percentages += ' o '
            else:
                percentages += '   '
        percentages += '\n'
    
    percentages += 4*' ' + (3*n+1)*'-' + '\n'
    l = max([len(item[0]) for item in expenses_tuple])
    for i in range(l):
        percentages += 5*' '
        for category in expenses_tuple:
            if i < len(category[0]):
                percentages += category[0][i] + 2*' '
            else:
                percentages += 3*' '
        if i+1 != l:
            percentages += '\n'


    return percentages
    

Hmm, when I changed it to range(11) in your original code, the test passed.

It, did!
Thanks a lot. Seems some of the other changes messed it up

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