Scientific Computing with Python Projects - Budget App

Tell us what’s happening:

Hi everbody. The code i provide works perfectly on VSCode but when i try to run it on freecode platform, it gives me an error " // running tests

create_spend_chart

should print a different chart representation. Check that all spacing is exact. // tests completed"
It’s very strange cause the output that vscode gives me is exactly that from the required specification.
Please help me, i just don’t know what to do! Thank u

Your code so far

class Category:
    def __init__(self, category):
        self.category = category
        self.ledger = []
    
    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, "description": description})
            return True
        else:
            return False
    
    def get_balance(self):
        return sum(i['amount'] for i in self.ledger)

    def transfer(self, amount, destination_category):
        if self.check_funds(amount):
            self.withdraw(amount,f"Transfer to {destination_category.category}")
            destination_category.deposit(amount, f"Transfer from {self.category}")
            return True
        else:
            return False
    
    def check_funds(self, amount):
        return amount <= self.get_balance()

    def __str__(self):
        title = self.category.center(30, '*')+'\n'
        transactions = ''
        for item in self.ledger:
            description = item['description'][:23].ljust(23)
            amount = "{:.2f}".format(item['amount']).rjust(7)
            transactions += f"{description}{amount}\n"
        total = "Total: {:.2f}".format(self.get_balance())
        return title+transactions+total  

def create_spend_chart(categories):
    # calculate spending percentages
    total_category_withdrawals = {}
    for category in categories:
        total_category_withdrawals[category.category] = sum(item['amount'] for item in category.ledger if item['amount'] < 0)
    
    total_spent = sum(total_category_withdrawals.values())
    percentages = {category: int(((total_category_withdrawals[category]*100 / total_spent)//10)*10) for category in total_category_withdrawals}
    
    ################create the chart####################
    # title
    chart = "Percentage spent by category" + "\n"
    # bar chart
    for i in range(100, -10, -10):
        chart += str(i).rjust(3) + '| '
        for category in categories:
            if percentages[category.category] >= i:
                chart += 'o  '
            else:
                chart += '   '
        chart += '\n'
    # horizontal line
    chart += "    " + "-" * (len(categories) * 3 + 1) + "\n"
    # horizontal labels
    max_name_length = max(len(category.category) for category in categories)
    for i in range(max_name_length):
        chart += "     "
        for category in categories:
            if i < len(category.category):
                chart += category.category[i] + '  '
            else:
                chart += '   '
        chart += '\n'
    
    print (chart)

        




    


    

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:123.0) Gecko/20100101 Firefox/123.0

Challenge Information:

Scientific Computing with Python Projects - Budget App

create_spend_chart is expected to return string with chart, regardless if it prints anything on it own or not.

The other detail - there’s additional new line character at the end of the chart, the expected version doesn’t have new line there.

Hi sanity. Thank u for your reply! However, I didn’t understand what exactly you mean by " create_spend_chart is expected to return string with chart, regardless if it prints anything on it own or not" . I apologize for that, maybe you’re referring to replacing “print(chart)” with “return chart” ?? And what about the new line character? I don’t recall him including it at the ends of the chart. Also why on vscode is the output exactly as desired?

Ok i fixed the code. I finally understood what you meant : i replaced print with return and an if statement to avoid going further to head after iterating over the length of the longest category. So : if (i < max_name_lenght -1): chart+= ‘\n’. Damn you ‘-1’ !! :joy:

1 Like

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