Build a Budget App Project - Build a Budget App Project

Tell us what’s happening:

BUDGET APP - FINAL TEST (3rd project) SCWP

Why is my answer showing wrong on the budget app chart thing? lemme share the output too. It is the final question for me in this project and I have finished the other 4 projects too! Would appreciate help :slight_smile:

Your code so far

Your browser information:

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

Challenge Information:

Build a Budget App Project - Build a Budget App Project

class Category:

def __init__(self,category):
    
    self.category = category
    self.total = 0 
    self.amount = 0 
    self.inital = 0 
    self.ledger = []

def deposit(self,amount,description = ''):

    self.amount = amount 
    self.description = description 
    self.total = amount
    self.initial = amount 

    self.dictionary = {'amount':self.amount,
                      'description':self.description}
    
    self.ledger.append(self.dictionary)

def withdraw(self,amount,description = ''):
    
    self.amount = amount
    self.description = description
    self.dictionary = {'amount':-1*self.amount,
                       
                      'description':self.description}
    
    if self.total < self.amount:
        return False
    else : 
        self.ledger.append(self.dictionary)
        self.total = self.total - self.amount
        return True
  
def get_balance(self):

    return self.total

def transfer(self,num,obj):
    self.num = num
    
    if self.total < self.num :
        return False
    else:
        self.withdraw(self.num,f'Transfer to {obj.category}') 
        obj.deposit(self.num,f'Transfer from {self.category}')
        return True

def __str__(self):

    self.string = self.category.center(30,'*') + '\n'

    for i in range(len(self.ledger)):

        self.string += (self.ledger)[i]['description'][0:23]  + ' '*(23 - len((self.ledger)[i]['description']) ) + ' '*(7-len(str("{:.2f}".format((self.ledger)[i]['amount']))))  + "{:.2f}".format((self.ledger)[i]['amount']) + '\n'

    self.string += 'Total: ' + str(self.total) 
    return self.string

def check_funds(self,number):
    
    self.number = number 
    
    if self.number > self.total:
        return False
    else:
        return True 
    
def percentage_spent(self):
      
    percentage = (self.total / self.initial ) * 100
    rounded_num = round(percentage,-1)

    return rounded_num

def create_spend_chart(categories):

    key_list = []
    value_list = []

    for x in categories:
            value_list.append(x.percentage_spent())
            key_list.append(x.category)

    arb = ''
    new_list = []
    
    for i in range(-100,10,10):
        
        if i == -100 :
               arb = str(i*-1) + '|'  
               new_list.append(arb)
        elif i == 0 :
               arb = "  "+ str(i*-1) + '|'  
               new_list.append(arb)
        else:
              arb = ' ' + str(i*-1) + '|' 
              new_list.append(arb)

    q = new_list[::-1]

    for i in value_list:
          for j in q:
                
                num_val = i / 10 
                index = q.index(j)
                
                if num_val >= index:
                      
                      if value_list.index(i) == 0:
                            q[index] += ' o'
                      else:
                            if 'o' in j:
                                  q[index] +=  '  o'
                            else:
                                  q[index] += 4*' ' +  'o'

    new_List1 = q[::-1]

    for i in new_List1:
          new_List1[new_List1.index(i)] += '\n'

    listToStr = ''.join([str(elem) for elem in new_List1])

    footer = '    '

    for i in value_list:
          footer +=  '-'*3 

    result = ""
 
    max_length = max(len(word) for word in key_list)
    leading_spaces = 5

    for i in range(max_length):
     result += " " * leading_spaces
 
     for word in key_list:
           if i < len(word):
                 result += word[i]
           else:
                 result += " "
          
           result += "  "
     
     result = result.rstrip() + "\n"

    result = result.rstrip()

    return print('Percentage spent by category' + '\n' + listToStr + footer + '\n' + result)


Please, post the entire code. Do not post it in portions.

The return value of print is None. So, you shouldn’t return a print call here.

1 Like

hey i solved the thing but had to redo the listToStr method. Also thanks a lot on the the print() thing ! :slight_smile:

1 Like

also had to fix the way of calculating the percentages

1 Like