Я пытаюсь решить проект, в котором я создаю класс категорий и функцию create_spend_chart, чтобы создать текстовую диаграмму трат. Пример, имена моей категории, напечатанные вертикально, не совсем совместно выровнены с столбцами, а иногда отсутствуют пробелы. < /p>
Вот мой код: < /p>
class Category:
def __init__(self, name):
self.name = 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, other_category):
if self.check_funds(amount):
self.withdraw(amount, f"Transfer to {other_category.name}")
other_category.deposit(amount, f"Transfer from {self.name}")
return True
return False
def check_funds(self, amount):
return amount = i:
line += " o "
else:
line += " "
chart += line + " \n"
# Add the bottom border
chart += " " + "-" * (len(categories) * 3 + 1) + "\n"
# Add the category names vertically below the bar
max_length = max(len(item["name"]) for item in category_spending)
for i in range(max_length):
line = " "
for item in category_spending:
if i < len(item["name"]):
line += item["name"] + " "
else:
line += " "
chart += line.rstrip() + "\n"
return chart.rstrip("\n")
# Example Usage
food = Category("Food")
clothing = Category("Clothing")
entertainment = Category("Entertainment")
food.deposit(1000, "Initial deposit")
food.withdraw(150.25, "Groceries")
food.withdraw(50.75, "Snacks")
clothing.deposit(500, "Initial deposit")
clothing.withdraw(150, "Jeans")
entertainment.deposit(800, "Initial deposit")
entertainment.withdraw(300, "Movies")
print(create_spend_chart([food, clothing, entertainment]))
Подробнее здесь: https://stackoverflow.com/questions/795 ... red-format
Почему мой выход Create_spend_chart не соответствует требуемому формату? [закрыто] ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Почему мой выход Create_spend_chart не соответствует требуемому формату? [закрыто]
Anonymous » » в форуме Python - 0 Ответы
- 5 Просмотры
-
Последнее сообщение Anonymous
-