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