Scientific Computing with Python Projects - Budget App

Tell us what’s happening:
I have completed the project “Budget app” and in all my testing of my code I run into no problems. However when i run it on .replit it messes up. The two test that my code fails are (test_create_spend_chart) and (test_to_string). A bunch of random “?”, “+” and “-” appear when the .replit test code runs. When i run these parts of the test code in my own IDE i get the expected result without the random characters.
I don’t know what I am missing that makes the code break when put into .replit.
Link to my .replit code: boilerplate-budget-app - Replit

Your code so far

class Category:

    def __init__(self, name):
        self.name = name
        self.ledger = []
        self.balance = bool(0)
        self.withdrawals = []
        self.deposits = []

    def get_balance(self):
        return self.balance

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

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

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


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

    def __str__(self):
        middle_num = int((30 - len(self.name))/2)
        output_text = ('*' * middle_num) + self.name + ('*' * middle_num) + '\n'
        for entry in self.ledger:
            entry_description = entry["description"]
            if len(entry_description) > 23:
                entry_description = entry_description[:23]
            number_len = len(str(entry["amount"]))
            spaces_to_add = 30 - number_len - len(entry_description)
            part_text = entry_description + (' ' * spaces_to_add) + str(entry["amount"]) + '\n'
            output_text = output_text + part_text

        total_text = 'Total: ' + str(self.balance)
        output_text = output_text + total_text
        return output_text

def check_percent(target, number):
    if number >= target:
        return "o"
    else:
        return " "

def create_spend_chart(categories):
    # Calculate Percent
    total_amount = 0
    for category in categories:
        total_amount = total_amount + abs(sum(category.withdrawals))
    percentage_list = []
    for category in categories:
        percent_of_number = (abs(sum(category.withdrawals)) / total_amount) * 100
        rounded_number = round(percent_of_number/10)*10
        if rounded_number > percent_of_number:
            rounded_number = rounded_number - 10
        percentage_list.append(rounded_number)

    # Sort by size
    sorted_percentage_list = []
    sorted_categories_list = []

    for x in range(0, len(percentage_list)):
        max_number = max(percentage_list)
        max_number_index = percentage_list.index(max_number)
        sorted_percentage_list.append(max_number)
        sorted_categories_list.append(categories[max_number_index].name)
        percentage_list.remove(max_number)
        categories.remove((categories[max_number_index]))

    # Create Text
    text_out = 'Percentage spent by category' + '\n'

    # 100
    text_out = text_out + '100| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(100, entry)) + '  '
    text_out = text_out + '\n'

    # 90
    text_out = text_out + ' 90| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(90, entry)) + '  '
    text_out = text_out + '\n'

    # 80
    text_out = text_out + ' 80| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(80, entry)) + '  '
    text_out = text_out + '\n'

    # 70
    text_out = text_out + ' 70| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(70, entry)) + '  '
    text_out = text_out + '\n'

    # 60
    text_out = text_out + ' 60| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(60, entry)) + '  '
    text_out = text_out + '\n'

    # 50
    text_out = text_out + ' 50| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(50, entry)) + '  '
    text_out = text_out + '\n'

    # 40
    text_out = text_out + ' 40| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(40, entry)) + '  '
    text_out = text_out + '\n'

    # 30
    text_out = text_out + ' 30| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(30, entry)) + '  '
    text_out = text_out + '\n'

    # 20
    text_out = text_out + ' 20| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(20, entry)) + '  '
    text_out = text_out + '\n'

    # 10
    text_out = text_out + ' 10| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(10, entry)) + '  '
    text_out = text_out + '\n'

    # 0
    text_out = text_out + '  0| '
    for entry in sorted_percentage_list:
        text_out = text_out + (check_percent(0, entry)) + '  '
    text_out = text_out + '\n'

    # Line
    text_out = text_out + '    -' + ('---' * len(sorted_percentage_list)) + '\n'

    # Letters
    longest_catagory = 0
    for x in sorted_categories_list:
        if len(x) > longest_catagory:
            longest_catagory = len(x)

    for x in range (0, longest_catagory):
        text_to_add = '     '
        for z in range (0, len(sorted_categories_list)):
            try:
                text_to_add = text_to_add + sorted_categories_list[z][x] + '  '
            except:
                text_to_add = text_to_add + '   '
        text_to_add = text_to_add + '\n'
        text_out = text_out + text_to_add

    return text_out

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Budget App

Link to the challenge:

The output isn’t “random”. Its telling you how your results differ from the required results.

- deposit                    900
?                         ---
+ deposit                 900.00
?                            +++
  milk, cereal, eggs, bac -45.67
- Transfer to Entertainme    -20
?                         ---
+ Transfer to Entertainme -20.00
?                            +++

The - before a line indicates some content that was expected but not found and the + before a line indicates some content that was found but not expected.

Here, it looks like if there are 0 cents, you aren’t printing any cents, but you need to.

Thank you for this information. Did not know that was what it was. Now I know how to interpret the errors and use it to find the problems in my code. Thanks!

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