Hello, I am stuck on step 18 and my console does not print it right. It does not print the 'o’s and the names for the categories are not under the chart but in the left side. Can someone see what I have done wrong?
Your code so far
class Category:
def __init__(self, name):
self.name = name
self.ledger = []
self.balance = 0
#Tilføj transaction til listen
def deposit(self, amount, description=''):
self.balance += amount
self.ledger.append({'amount': amount, 'description': description})
#Træk fra kontoen
def withdraw(self, amount, description=''):
if self.check_funds(amount):
self.deposit(-amount, description)
return True
else:
return False
def get_balance(self):
return self.balance
def transfer(self, amount, another_category):
if self.check_funds(amount):
self.balance -= amount
self.ledger.append({'amount': -amount, 'description': f'Transfer to {another_category.name}'})
another_category.deposit(amount, f'Transfer from {self.name}')
return True
else:
return False
def check_funds(self, amount):
if amount <= self.balance:
return True
else:
return False
def __str__(self):
output = self.name.center(30,'*') + '\n'
for item in self.ledger:
amount = item['amount']
description = item['description']
output += f'{description[0:23]:23}{amount:7.2f}'
output += '\n'
output += 'Total: '
output += format(self.balance, '.2f')
return output
def create_spend_chart(categories):
categories_name = []
categories_procent = []
chartstr = 'Percentage spent by category\n'
#beregning af procentdele
total_withdrawal = 0
for category in categories:
procent = 0
categories_name.append(category.name)
for item in category.ledger:
amount = item['amount']
if amount < 0:
total_withdrawal += -amount
procent += (round(-amount/total_withdrawal/10))*1000
categories_procent.append(procent)
else:
None
#Lav grafen
for num in range(100, 0, -10):
chartstr += f"{str(num) + '|':>4}"
for procent in categories_procent:
if procent >= num:
chartstr += 'o '
else:
chartstr += ' '
chartstr += '\n'
chartstr += ' ' + (3*len(categories)+2)*'-' + '\n'
#Indsæt kategorinavne
max_length = max(len(category) for category in categories_name)
for i in range(0, max_length):
lodret_navne = ' '
for category in categories_name:
if i < len(category):
lodret_navne += category[i] + ' '
else:
lodret_navne += ' '
lodret_navne += '\n'
chartstr += lodret_navne
return chartstr
food = Category('Food')
food.deposit(1000, 'Initial deposit')
food.withdraw(10.15, 'Groceries')
food.withdraw(15.89, 'Restaurant')
clothing = Category('Clothing')
food.transfer(50, clothing)
clothing.withdraw(30,'Trousers')
print(food)
print("\n")
print(clothing)
print("\n")
print(create_spend_chart([food,clothing]))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15
That makes it look like there’s new line character added after each letter in the category name. Which part of code is responsible for the category names?
Okay so now I moved the new line character one indentation out and now the console looks like this, but I still can’t figure out why it won’t print the ‘o’s
for num in range(100, 0, -10):
chartstr += f"{str(num) + '|':>4}"
for procent in categories_procent:
if procent >= num:
chartstr += 'o '
else:
chartstr += ' '
chartstr += '\n'
chartstr += ' ' + (3*len(categories)+2)*'-' + '\n'
What does it mean that no os are printed from this code - on which values that depends on? Following this further, what exactly are these values? Are they correct?
Now I changed the categories_procent(percentages of the different categories) to a dictionary(with name of category and percent) and then used the values(percent) to make the ‘o’s
Hi, I tried printing the list of the percentages, the names of the categories and the total withdrawal now, it seems like the problem is with the calculation of the percentages but I can’t figure out where I made a mistake. But I can’t understand why it has five different things in the percentages list when I only have 3 categories
I have come to the conclusion that the problem is that every time a withdrawal is found in a category the percentage is added to the list, but if there are multiple withdrawals within one category it does not sum those percentages but just adds it as another percentage in the list. How do I fix that?
consider if you should calculate the percentage for each single withdrawal, or if you maybe want to first calculate how much is withdrawn in each category, and then calculate the percentage
you are currently doing this for the total withdrawal of all categories, aren’t you?