Tell us what’s happening:
Describe your issue in detail here.
create_spend_chart
should print a different chart representation. Check that all
spacing is exact.
The only x I got is this question. how to fix the problem? Please Help.
Your code so far
class Category:
def __init__(self, category_name):
self.category_name = category_name
self.ledger = []
def deposit(self, amount, description = ''):
self.ledger.append({"amount": amount, "description": description})
def withdraw(self, amount, description = ''):
if self.check_funds(amount):
self.ledger.append({"amount": -amount, "description": description})
return True
return False
def get_balance(self):
return sum(item['amount'] for item in self.ledger)
def transfer(self, amount, destination_category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {destination_category.category_name}")
destination_category.deposit(amount, f"Transfer from {self.category_name}")
return True
return False
def check_funds(self, amount):
return amount <= self.get_balance()
def __str__(self):
länge = int((30 - len(self.category_name)) / 2)
lines = [f"{'*' * länge}{self.category_name}{'*' * länge}"]
total = 0
for entry in self.ledger:
description = entry['description'][:23]
amount = '{:.2f}'.format(entry['amount'])
lines.append(f'{description:<23}{amount:>7}')
total += entry['amount']
lines.append(f'Total: {total:.2f}') # Korrigiert die Ausgabe der Gesamtsumme
return '\n'.join(lines)
def create_spend_chart(categories):
category_name = []
spent = []
for category in categories:
total_spent = sum(item['amount'] for item in category.ledger if item['amount'] > 0)
category_name.append(category.category_name)
spent.append(total_spent)
total_spent = sum(spent)
percentages = [(amount / total_spent) * 100 for amount in spent]
chart = 'Percentage spent by category \n'
for i in range(100, -1, -10):
chart += f'{i:3}| '
for percent in percentages:
if percent >= i:
chart += 'o '
else:
chart += ' '
chart += '\n'
chart += ' ' + '-' * (len(categories) * 3 + 1) + '\n'
max_name_length = max(len(name) for name in category_name)
for i in range(max_name_length):
chart += ' '
for name in category_name:
if i < len(name):
chart += f'{name[i]} '
else:
chart += ' '
if i != max_name_length:
chart += '\n'
return chart
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 Safari/605.1.15
Challenge Information:
Scientific Computing with Python Projects - Budget App