Tell us what’s happening:
My system and browser do not provide F12 and its corresponding features.
Thank you.
Lesson URL (copy - paste from your browser’s address bar)
Welcome to the forum @dizzysearchr,
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge.
The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
Thank you.
what is your system and browser?
I’m stuck at test No 17.
My system and browser don’t provide F12 with its features.
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36
Build a Budget App Project - Build a Budget App Project
We need to see your code to help you.
There are two ways you can format your code to make it easier to read and test:
</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:
what is your system and browser?
A search on your User Agent indicates you are using Google Chrome version 153 on a 64-bit Linux desktop, using the Blink. If that is true, have you tried Ctrl+Shift+J to access the browser’s console?
Hi,
it’s an Android-like OS on a tablet computer, browser is Vanadium.
class Category:
instances = []
def __init__(self, category):
self.category = category
self.ledger = []
Category.instances.append(self)
def deposit(self, amount, description = ''):
self.ledger += [{'amount': amount, 'description': description}]
def withdraw(self, amount, description=''):
if self.check_funds(amount):
self.ledger += [{'amount': -abs(amount), 'description': description}]
return True
else:
return False
def get_balance(self):
return sum([ledger_elem['amount'] for ledger_elem in self.ledger])
def transfer(self, amount, other_cat):
if not self.check_funds(amount):
return False
else:
# Adding withdrawal to ledger of category object
self.ledger += [{'amount': -abs(amount), 'description': f'Transfer to {other_cat.category}'}]
# Adding deposit to other budget category's ledger
other_cat.ledger += [{'amount': amount, 'description': f'Transfer from {self.category}'}]
return True
def check_funds(self, amount):
if amount > self.get_balance():
return False
else:
return True
def __str__(self):
budget_obj_dis = f'{self.category:*^30}\n'
for indx_one, item in enumerate([ledger_item['description'] for ledger_item in self.ledger]):
for indx_two, iteem in enumerate([ledger_iteem['amount'] for ledger_iteem in self.ledger]):
while indx_one == indx_two:
budget_obj_dis += '{:23s}{:>7.2f}\n'.format(item[:23], iteem)
break
budget_obj_dis += f'Total: {self.get_balance()}'
return budget_obj_dis
@classmethod
def get_instances(cls):
return cls.instances
def create_spend_chart(categories):
bar_chart = ''
title = 'Percentage spent by category'
bar_chart += f'{title}\n'
lines_list = []
for label in range(100, -5, -10):
line_name = f'{label}_line'
line_name = line_name.split()
lines_list += line_name
tot_expenses = 0
expenses_by_cat = dict()
for categ in categories:
for key in expenses_by_cat:
key = categ
expenses_by_cat[categ] = 0
for instance in Category.instances:
expenses_by_cat[instance.category] = expenses_by_cat[categ]
# neg_amounts = list(filter(lambda x: x == -abs(x), instance.ledger[0]['amount']))
ledger_amounts = [ledger_dict.get('amount') for ledger_dict in instance.ledger]
neg_amounts = list(filter(lambda x: x == -abs(x), ledger_amounts))
expenses_by_cat[instance.category] += abs(sum(neg_amounts))
tot_expenses += expenses_by_cat[instance.category]
# Calculating percentage
for i in expenses_by_cat:
perc_cat = int(round((expenses_by_cat[i] * 100 / tot_expenses), -1))
expenses_by_cat[i] = perc_cat
del_post = '|'
bar = 'o'
for bar_line in lines_list:
bar_line = '{:>3}'.format(bar_line.strip('_line'))
for indx, perc_val in enumerate(expenses_by_cat.values()):
if indx == 0 and (perc_val >= int(bar_line)):
bar_line += del_post + ' ' + bar + ' '
elif indx == 0 and not (perc_val >= int(bar_line)):
bar_line += del_post + ' '
elif indx == 1 and (perc_val >= int(bar_line[:3])):
bar_line += '' + bar + ' '
elif indx == 1 and not (perc_val >= int(bar_line[:3])):
bar_line += ' '
elif indx == 2 and (perc_val >= int(bar_line[:3])):
bar_line += '' + bar + ' '
elif indx == 2 and not (perc_val >= int(bar_line[:3])):
bar_line += ' '
elif indx == 3 and (perc_val >= int(bar_line[:3])):
bar_line += '' + bar + ' '
elif indx == 3 and not (perc_val >= int(bar_line[:3])):
bar_line += ' '
bar_chart += f'{bar_line}\n'
lines_list.append('')
lines_list.append('---')
bar_chart += f' {lines_list[-1] * len(Category.instances)}{"-"}'
cat_name_len_list = []
for cat_name in expenses_by_cat.keys():
cat_name_len_list.append(len(cat_name))
char_lines_list = []
for i in range(max(cat_name_len_list)):
char_lines_list.append('')
char_line_len_list = []
for char_line_number, char_line in enumerate(char_lines_list):
for cat_number, cat in enumerate(expenses_by_cat.keys()):
for letter_number, letter in enumerate(cat):
while char_line_number == letter_number:
if cat_number == 0:
char_line += f'{letter} '
elif cat_number == 1 and not char_line:
char_line += f' {letter} '
elif cat_number == 1 and char_line:
char_line += f'{letter} '
elif cat_number == 2 and len(char_line) == 6:
char_line += f'{letter} '
elif cat_number == 2 and len(char_line) == 3:
char_line += f' {letter} '
elif cat_number == 2 and not char_line:
char_line += f' {letter} '
elif cat_number == 3 and len(char_line) == 9:
char_line += f'{letter} '
elif cat_number == 3 and len(char_line) == 6:
char_line += f' {letter} '
elif cat_number == 3 and len(char_line) == 3:
char_line += f' {letter} '
elif cat_number == 3 and not char_line:
char_line += f' {letter} '
char_line_len_list.append(len(char_line))
if len(char_line) < max(char_line_len_list):
pad_char_len = max(char_line_len_list) - len(char_line)
char_line += f'{" " * pad_char_len}'
bar_chart += f'\n {char_line}'
return bar_chart
Test your code:
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)
print(create_spend_chart([food,clothing]))
What do you see in the fCC editor’s console pane?
Were you asked to create a class variable?
No, I wasn’t, however the class variable helped me to develop the code.
As for the result in the console pane, I’ll be giving you some feedback tomorrow.
Thank you.
You shouldn’t need it since you are expected to pass an array of category instances to the create_spend_chart function, and I’m not sure the tests will support that.
All I see in the console output is the string representation of the ‘food’ object. There is no output of the spend chart. And not a single test passes now.
Please share your updated code so we can take a look.
I just added
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)
print(create_spend_chart(\[food,clothing\]))
after my code in the editor to test it, as you suggested. Apart from that, I made no changes.
Did you see this type error in the console?
Traceback (most recent call last):
File "main.py", line 149, in <module>
File "main.py", line 106, in create_spend_chart
TypeError: object of type 'Category' has no len()
I can see it now, yes. Definitely.
Hi,
I did some debugging. Now, the tests keep running and running, without end.
Once, I got an error message in the console:
Error: Timed out waiting for the test worker to initialize
But each attempt to “Check your code” results in the tests running infinitely.
please post your code so we can check if it’s an issue with that
class Category:
# instances = []
def __init__(self, category):
self.category = category
self.ledger = []
# Category.instances.append(self)
def deposit(self, amount, description = ''):
self.ledger += [{'amount': amount, 'description': description}]
def withdraw(self, amount, description=''):
if self.check_funds(amount):
self.ledger += [{'amount': -abs(amount), 'description': description}]
return True
else:
return False
def get_balance(self):
return sum([ledger_elem['amount'] for ledger_elem in self.ledger])
def transfer(self, amount, other_cat):
if not self.check_funds(amount):
return False
else:
# Adding withdrawal to ledger of category object
self.ledger += [{'amount': -abs(amount), 'description': f'Transfer to {other_cat.category}'}]
# Adding deposit to other budget category's ledger
other_cat.ledger += [{'amount': amount, 'description': f'Transfer from {self.category}'}]
return True
def check_funds(self, amount):
if amount > self.get_balance():
return False
else:
return True
def __str__(self):
budget_obj_dis = f'{self.category:*^30}\n'
for indx_one, item in enumerate([ledger_item['description'] for ledger_item in self.ledger]):
for indx_two, iteem in enumerate([ledger_iteem['amount'] for ledger_iteem in self.ledger]):
while indx_one == indx_two:
budget_obj_dis += '{:23s}{:>7.2f}\n'.format(item[:23], iteem)
break
budget_obj_dis += f'Total: {self.get_balance()}'
return budget_obj_dis
'''
@classmethod
def get_instances(cls):
return cls.instances
'''
def create_spend_chart(categories):
bar_chart = ''
title = 'Percentage spent by category'
bar_chart += f'{title}\n'
lines_list = []
for label in range(100, -5, -10):
line_name = f'{label}_line'
line_name = line_name.split()
lines_list += line_name
tot_expenses = 0
expenses_by_cat = dict()
for instance in categories:
for key in expenses_by_cat:
key = instance.category
expenses_by_cat[instance.category] = 0
# expenses_by_cat[instance.category] = expenses_by_cat[categ]
ledger_amounts = [ledger_dict.get('amount') for ledger_dict in instance.ledger]
neg_amounts = list(filter(lambda x: x == -abs(x), ledger_amounts))
expenses_by_cat[instance.category] += abs(sum(neg_amounts))
tot_expenses += expenses_by_cat[instance.category]
# Calculating percentage
for i in expenses_by_cat:
perc_cat = int(round((expenses_by_cat[i] * 100 / tot_expenses), -1))
expenses_by_cat[i] = perc_cat
del_post = '|'
bar = 'o'
for bar_line in lines_list:
bar_line = '{:>3}'.format(bar_line.strip('_line'))
for indx, perc_val in enumerate(expenses_by_cat.values()):
if indx == 0 and (perc_val >= int(bar_line)):
bar_line += del_post + ' ' + bar + ' '
elif indx == 0 and not (perc_val >= int(bar_line)):
bar_line += del_post + ' '
elif indx == 1 and (perc_val >= int(bar_line[:3])):
bar_line += '' + bar + ' '
elif indx == 1 and not (perc_val >= int(bar_line[:3])):
bar_line += ' '
elif indx == 2 and (perc_val >= int(bar_line[:3])):
bar_line += '' + bar + ' '
elif indx == 2 and not (perc_val >= int(bar_line[:3])):
bar_line += ' '
elif indx == 3 and (perc_val >= int(bar_line[:3])):
bar_line += '' + bar + ' '
elif indx == 3 and not (perc_val >= int(bar_line[:3])):
bar_line += ' '
bar_chart += f'{bar_line}\n'
lines_list.append('')
lines_list.append('---')
bar_chart += f' {lines_list[-1] * len(categories)}{"-"}'
cat_name_len_list = []
for cat_name in expenses_by_cat.keys():
cat_name_len_list.append(len(cat_name))
char_lines_list = []
for i in range(max(cat_name_len_list)):
char_lines_list.append('')
char_line_len_list = []
for char_line_number, char_line in enumerate(char_lines_list):
for cat_number, cat in enumerate(expenses_by_cat.keys()):
for letter_number, letter in enumerate(cat):
while char_line_number == letter_number:
if cat_number == 0:
char_line += f'{letter} '
elif cat_number == 1 and not char_line:
char_line += f' {letter} '
elif cat_number == 1 and char_line:
char_line += f'{letter} '
elif cat_number == 2 and len(char_line) == 6:
char_line += f'{letter} '
elif cat_number == 2 and len(char_line) == 3:
char_line += f' {letter} '
elif cat_number == 2 and not char_line:
char_line += f' {letter} '
elif cat_number == 3 and len(char_line) == 9:
char_line += f'{letter} '
elif cat_number == 3 and len(char_line) == 6:
char_line += f' {letter} '
elif cat_number == 3 and len(char_line) == 3:
char_line += f' {letter} '
elif cat_number == 3 and not char_line:
char_line += f' {letter} '
char_line_len_list.append(len(char_line))
if len(char_line) < max(char_line_len_list):
pad_char_len = max(char_line_len_list) - len(char_line)
char_line += f'{" " * pad_char_len}'
bar_chart += f'\n {char_line}'
return bar_chart
"""
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)
print(create_spend_chart([food, clothing]))
"""