Tell us what’s happening:
I don’t really understand what the tests are asking of me, nor do I fully understand the output of the F12 console. Also, for some strange reason, while I have checked and double checked that the food category has the appropriate percentage, for some reason, the bar for it is showing a higher percentage, and I’m not sure why.
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
def deposit(self, amount, description = ""):
self.deposit = amount
self.description = description
self.balance += amount
self.ledger.append({'amount': amount, 'description': description})
def withdraw(self, amount, description = ""):
self.withdrawal = amount
self.description = description
if self.check_funds(amount):
self.balance -= amount
self.ledger.append({'amount': -amount, 'description': description})
return True
else:
return False
def transfer(self, amount, other_category):
if self.check_funds(amount) == True:
self.withdraw(amount, f'Transfer to {other_category.name}')
other_category.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def get_balance(self):
return self.balance
def check_funds(self, amount):
self.amount = amount
if amount <= self.balance:
return True
else:
return False
def get_ledger(self):
return self.ledger
def __str__(self):
category = self.name
ledger_title = ""
ledger_title += f"{category:*^30}"
ledger = self.ledger
ledger_sections = ""
descriptions = []
amounts = []
for i, records in enumerate(ledger):
descriptions = records["description"]
amounts = records["amount"]
if len(descriptions) > 23:
descriptions = descriptions[0:23]
amounts = f"{amounts:.2f}"
descriptions = f"{descriptions:23}"
ledger_sections += f"{descriptions:.23}" + f"{amounts:>7}" + "\n"
return f"{ledger_title}\n" + f"{ledger_sections}\nTotal: {self.balance:.2f}\n"
def create_spend_chart(categories):
chart = []
categorylisting = []
names = ""
spentAmount = 0
spendingList = []
numbers = []
ledger_amounts = []
chart_title = "Percentage spent by category\n"
# getting category names
for category in categories:
categorylisting.append(category.name)
names += category.name + " "
for items in category.ledger:
if items['amount'] < 0:
spentAmount += abs(items['amount'])
spendingList.append(abs(items['amount']))
for items in category.ledger:
numbers.append(items['amount'])
totalbalance = sum(numbers)
percentages = [round(num / totalbalance * 100) * 10 for num in spendingList]
y_axis = ""
x_axis_line = " " + "-" * (len(category.name) + 1)
percentbycategory = dict(zip(categorylisting, percentages))
for num in range(100, -10, -10):
if num == 100:
y_axis += f"{num}|#"
if num >0 and num < 100:
y_axis += f" {num}|#"
if num == 0:
y_axis += f" {num}|#"
y_values = y_axis.split("#")
for percent in percentbycategory.values():
if percent == 100:
y_values[0] += " o"
if percent >=90:
y_values[1] += " o"
if percent >=80:
y_values[2] += " o"
if percent >=70:
y_values[3] += " o"
if percent >=60:
y_values[4] += " o"
if percent >=50:
y_values[5] += " o"
if percent >=40:
y_values[6] += " o"
if percent >=30:
y_values[7] += " o"
if percent >=20:
y_values[8] += " o"
if percent >=10:
y_values[9] += " o"
if percent >=0:
y_values[10] += " o"
y_axis_line = "\n" "".join(y_values)
max_chars = max(len(category.name) for category in categories)
x_axis = ""
for i in range(0, max_chars):
x_axis += " "
for category in categories:
if len(category.name) > i:
x_axis += category.name[i] + " "
else:
x_axis += " "
x_axis += "\n"
chart = f"{chart_title}{y_axis_line}{x_axis_line}\n{x_axis} "
return chart
food = Category('Food')
food.deposit(900, 'deposit')
food.withdraw(45.67, 'milk, cereal, eggs, bacon, bread')
clothing = Category('Clothing')
food.transfer(50, clothing)
entertainment = Category('Entertainment')
food.transfer(20, entertainment)
print(food)
print(clothing)
print(entertainment)
spendChart = create_spend_chart([food, clothing, entertainment])
print(spendChart)
The current messages are as follows:
// running tests
16. Printing a Category instance should give a different string representation of the object.
19. The height of each bar on the create_spend_chart chart should be rounded down to the nearest 10.
20. Each line in create_spend_chart chart should have the same length. Bars for different categories should be separated by two spaces, with additional two spaces after the final bar.
21. create_spend_chart should correctly show horizontal line below the bars. Using three - characters for each category, and in total going two characters past the final bar.
23. create_spend_chart chart should have each category name written vertically below the bar. Each line should have the same length, each category should be separated by two spaces, with additional two spaces after the final category.
24. create_spend_chart should print a different chart representation. Check that all spacing is exact. Open your browser console with F12 for more details.
// tests completed
// console output
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Challenge Information:
Build a Budget App - Build a Budget App

