Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

need help to solve the code , i need assistant can u help

Your code so far

import math
class Category:
    def __init__(self,name):
        self.name=name
        self.ledger=[]
        self.budget=0
        self.title=''
    def __str__(self):
        body=''
        total=0
        if len(self.title)%2==0:
            self.title=self.name
        else:
            self.title=self.name+'*'
        for i in range((30-len(self.title))//2):
            self.title='*'+self.title+'*'
        for trans in self.ledger: 
            for i in range(23):
                try :
                    body+=trans['description'][i]
                except IndexError:
                    body+=' '
            numba='       '
            if str(trans['amount']).find('.')==-1:
                numba=str(trans['amount'])+'.00'
            else:
                numba=str(trans['amount'])
            while len(numba)<7:
                numba=' '+numba
            
            body+=f'{numba}\n'
            total+=trans['amount']
         
        return f'{self.title}\n{body}Total: {total}'

    def check_funds(self,amo):
        if amo<=self.budget:
            return True
        return False

    def deposit(self,amo,des=''):
        self.ledger.append({'amount': amo, 'description': des})
        self.budget+=amo

    def withdraw(self,amo,des=''):
        if self.check_funds(amo):
            self.ledger.append({'amount': 0-amo, 'description': des})
            self.budget=self.budget-amo
            return True  
        return False
    
    def get_balance(self):
        return self.budget

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

def create_spend_chart(categories):
    print('Percentage spent by category')
    j=0
    catspent=[]
    catpercen=[]
    totspent=0
    #fills catspent with the category name(on 'cat':) and its respective spent money(on 'spe':) and defines total money spent as sum of all spent money
    for category in categories:
        catspent.append({'cat':category.name,'spe':0})
        for trans in category.ledger:
            if trans['amount']<0:
                catspent[j]['spe']=catspent[j]['spe']-trans['amount']
        totspent+=catspent[j]['spe']
        j+=1

    #fills catpercen with category name and its respective percentage of spent money of the total spent by all categories
    for i in catspent:
        catpercen.append({'cat':i['cat'],'perc':(math.floor((i['spe']/totspent)*10))*10})

    #prints lines above the ---------
    k=100
    while k>=0:
        line=''
        if k<100:
            line+=' '
        if k<10:
            line+=' '
        line+=f'{k}| '
        
        line+=''.join('o  ' if cat['perc']>=k else '   ' for cat in catpercen)
        k=k-10
        print(line)

    #prints the ---------
    line='    -'
    for cat in catspent:
        line+='---'
    print(line)

    #prints the lines under the ---------
    maxr=0
    for i in catspent:
        if len(i['cat'])>maxr:
            maxr=len(i['cat'])
    for i in range(maxr):
        line='     '
        line+=''.join(f"{cat['cat'][i]}  " if i<=(len(cat['cat'])-1) else '   ' for cat in catpercen)
        print(line)


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)
print(food)
auto=Category('Auto')
auto.deposit(100,'hum')
auto.withdraw(50,'yeah')
print(auto)

create_spend_chart([food,clothing,auto])




Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

looking at the browser console

AssertionError: None
             != 'Percentage spent by category\n100|      [384 chars] t  '
: Expected different chart representation. Check that all spacing is exact.

your function returns None

def create_spend_chart(categories):
    chart = 'Percentage spent by category\n'
    j = 0
    catspent = []
    catpercen = []
    totspent = 0
    
    # Collect the category name and its respective spent money
    for category in categories:
        catspent.append({'cat': category.name, 'spe': 0})
        for trans in category.ledger:
            if trans['amount'] < 0:
                catspent[j]['spe'] = catspent[j]['spe'] - trans['amount']
        totspent += catspent[j]['spe']
        j += 1

    # Calculate the percentage spent for each category
    for i in catspent:
        catpercen.append({'cat': i['cat'], 'perc': (math.floor((i['spe'] / totspent) * 10)) * 10})

    # Create the chart lines
    k = 100
    while k >= 0:
        line = ''
        if k < 100:
            line += ' '
        if k < 10:
            line += ' '
        line += f'{k}| '

        line += ''.join('o  ' if cat['perc'] >= k else '   ' for cat in catpercen)
        chart += line + '\n'
        k -= 10

    # Add the separator line
    line = '    -'
    for cat in catspent:
        line += '---'
    chart += line + '\n'

    # Add the category names under the separator
    maxr = 0
    for i in catspent:
        if len(i['cat']) > maxr:
            maxr = len(i['cat'])
    for i in range(maxr):
        line = '     '
        line += ''.join(f"{cat['cat'][i]}  " if i < len(cat['cat']) else '   ' for cat in catpercen)
        chart += line + '\n'

    # Return the final chart
    return chart
def create_spend_chart(categories):
    chart = 'Percentage spent by category\n'
    j = 0
    catspent = []
    catpercen = []
    totspent = 0
    
    # Collect the category name and its respective spent money
    for category in categories:
        catspent.append({'cat': category.name, 'spe': 0})
        for trans in category.ledger:
            if trans['amount'] < 0:
                catspent[j]['spe'] = catspent[j]['spe'] - trans['amount']
        totspent += catspent[j]['spe']
        j += 1

    # Calculate the percentage spent for each category
    for i in catspent:
        catpercen.append({'cat': i['cat'], 'perc': (math.floor((i['spe'] / totspent) * 10)) * 10})

    # Create the chart lines
    k = 100
    while k >= 0:
        line = ''
        if k < 100:
            line += ' '
        if k < 10:
            line += ' '
        line += f'{k}| '

        line += ''.join('o  ' if cat['perc'] >= k else '   ' for cat in catpercen)
        chart += line + '\n'
        k -= 10

    # Add the separator line
    line = '    -'
    for cat in catspent:
        line += '---'
    chart += line + '\n'

    # Add the category names under the separator
    maxr = 0
    for i in catspent:
        if len(i['cat']) > maxr:
            maxr = len(i['cat'])
    for i in range(maxr):
        line = '     '
        line += ''.join(f"{cat['cat'][i]}  " if i < len(cat['cat']) else '   ' for cat in catpercen)
        chart += line + '\n'

    # Return the final chart
    return chart

I an fornatting your code for the second time, please follow the instructions on how to do that yourself

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

still iam facing the issue

class Category:

    
    
    def __init__(self,category):
        
        self.category = category
        self.ledger = []
        
    
        
    def __str__(self):
        topic_line = self.category.center(30,'*')
        ledger_lines = ''
        for line in self.ledger:
            description_string = ''
            if len(line['description']) > 23:
                description_string = line['description'][:23]
            else:
                description_string = line['description']
            
            amount_string = str((line['amount']))

            if '.' not in amount_string:
                amount_string += '.00'
            if len(amount_string) > 7:
                amount_string = amount_string[:7]
            amount_string = amount_string.rjust(30-len(description_string))

            ledger_str = description_string + amount_string
            ledger_lines += ledger_str + '\n'
        
        return f'{topic_line}\n{ledger_lines}Total: {self.get_balance()}'
       
        
    
    def deposit(self, amount, description = ''):    
        deposit = {}
        deposit['amount'] = amount
        deposit['description'] = description
        self.ledger.append(deposit)


    def withdraw(self, amount,description =''):
        withdrawal = {}
        withdrawal['amount'] = -amount
        withdrawal['description'] = description
        if self.check_funds(amount) is False:
            return False
        else:
            self.ledger.append(withdrawal)
            return True
    
    def get_balance(self):
        balance = 0
        for changes in self.ledger:
            balance += changes['amount']
        return balance

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

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

    

def create_spend_chart(categories):
    category_withdrawals = {}
    category_percentages = {}
    category_name_length  = 0        
    category_names = []
    categories_amount = 0
    for category in categories:
        category_names.append(category.category)
        categories_amount += 1
        if len(category.category) > category_name_length:
            category_name_length = len(category.category)
    
    categories_total_withdrawals = 0
    for category in categories:
        
        category_withdrawals[category.category] = 0
        for transaction in category.ledger:
            if transaction['amount'] < 0:
                category_withdrawals[category.category] += transaction['amount']
                categories_total_withdrawals += transaction['amount']

    for category in categories:
        category_percentage = 100*(category_withdrawals[category.category]/categories_total_withdrawals)// 10
        category_percentages[category.category] = category_percentage*10
    
    percentage_display = 'Percentage spent by category\n'
    for percentage in range(100, 0-1, -10):
        percentage_display += str(percentage).rjust(3)+'|'
        for category in categories:
            if category_percentages[category.category]>= percentage:
                percentage_display += ' o '
            else:
                percentage_display += '   '
        percentage_display += '\n'
    percentage_display += '    -'

    for category in categories:
        percentage_display += '---'
   
    
    for i in range(0, category_name_length):
        percentage_display += '\n     '
                
        for name in category_names:
            
            if i < len(name):
                percentage_display += name[i] +'  '
            else:
                percentage_display += '   '
     
    return f'{percentage_display.rstrip()}'  
    
    
    
     
    


food = Category('Food')
clothing = Category('Clothing')
auto = Category('Auto')
food.deposit(1000, 'deposit')
clothing.deposit(1000)
auto.deposit(1000)
food.withdraw(601)
clothing.withdraw(250)
auto.withdraw(150)







category_list = [food, clothing, auto]
print(create_spend_chart(category_list))
class Category:

    
    
    def __init__(self,category):
        
        self.category = category
        self.ledger = []
        
    
        
    def __str__(self):
        topic_line = self.category.center(30,'*')
        ledger_lines = ''
        for line in self.ledger:
            description_string = ''
            if len(line['description']) > 23:
                description_string = line['description'][:23]
            else:
                description_string = line['description']
            
            amount_string = str((line['amount']))

            if '.' not in amount_string:
                amount_string += '.00'
            if len(amount_string) > 7:
                amount_string = amount_string[:7]
            amount_string = amount_string.rjust(30-len(description_string))

            ledger_str = description_string + amount_string
            ledger_lines += ledger_str + '\n'
        
        return f'{topic_line}\n{ledger_lines}Total: {self.get_balance()}'
       
        
    
    def deposit(self, amount, description = ''):    
        deposit = {}
        deposit['amount'] = amount
        deposit['description'] = description
        self.ledger.append(deposit)


    def withdraw(self, amount,description =''):
        withdrawal = {}
        withdrawal['amount'] = -amount
        withdrawal['description'] = description
        if self.check_funds(amount) is False:
            return False
        else:
            self.ledger.append(withdrawal)
            return True
    
    def get_balance(self):
        balance = 0
        for changes in self.ledger:
            balance += changes['amount']
        return balance

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

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

    

def create_spend_chart(categories):
    category_withdrawals = {}
    category_percentages = {}
    category_name_length  = 0        
    category_names = []
    categories_amount = 0
    for category in categories:
        category_names.append(category.category)
        categories_amount += 1
        if len(category.category) > category_name_length:
            category_name_length = len(category.category)
    
    categories_total_withdrawals = 0
    for category in categories:
        
        category_withdrawals[category.category] = 0
        for transaction in category.ledger:
            if transaction['amount'] < 0:
                category_withdrawals[category.category] += transaction['amount']
                categories_total_withdrawals += transaction['amount']

    for category in categories:
        category_percentage = 100*(category_withdrawals[category.category]/categories_total_withdrawals)// 10
        category_percentages[category.category] = category_percentage*10
    
    percentage_display = 'Percentage spent by category\n'
    for percentage in range(100, 0-1, -10):
        percentage_display += str(percentage).rjust(3)+'|'
        for category in categories:
            if category_percentages[category.category]>= percentage:
                percentage_display += ' o '
            else:
                percentage_display += '   '
        percentage_display += '\n'
    percentage_display += '    -'

    for category in categories:
        percentage_display += '---'
   
    
    for i in range(0, category_name_length):
        percentage_display += '\n     '
                
        for name in category_names:
            
            if i < len(name):
                percentage_display += name[i] +'  '
            else:
                percentage_display += '   '
     
    return f'{percentage_display.rstrip()}'  
    
    
    
     
    


food = Category('Food')
clothing = Category('Clothing')
auto = Category('Auto')
food.deposit(1000, 'deposit')
clothing.deposit(1000)
auto.deposit(1000)
food.withdraw(601)
clothing.withdraw(250)
auto.withdraw(150)







category_list = [food, clothing, auto]
print(create_spend_chart(category_list))

I am trying different methods of code , i got the output but still iam getting error

your code is not readable , without a mod it would be impossible to read. Please format your code on your own

hsve you opened the browser console as written there?

THANK YOU SO MUCH, I GOT IT