Sorry to ask for help on this topic that has already appeared on the forum before. But I am not able to take advantage of other people’s solutions to solve my case.
In principle, the function prints everything fine and 403 characters. But I can’t pass the test, and I’m not able to know why.
Thanks in advance
Greetings
def create_spend_chart(categories):
num_categories = len(categories)
total_expenses = 0
resume_categories = []
num_characters = 0
# collecting data
for category in categories:
expenses = 0
for note in category.ledger:
if note['amount'] < 0:
expenses += abs(note['amount'])
total_expenses += expenses
resume_categories.append({'name': category.name, 'expenses': expenses})
num_characters = max(num_characters, len(category.name))
# printing information
groups = 11 # y axis for scale
pad = 4
letter_index = 0
lines = ['Percentage spent by category']
for line in range(12 + num_characters):
if line < groups:
partial = [f'{100 - line * 10}|'.rjust(pad, " ")]
for category in resume_categories:
category['%expenses'] = round_down(
category['expenses'] / total_expenses * 100, -1)
if (100 - line * 10) <= category['%expenses']:
partial.append('o ')
else:
partial.append(' ')
lines.append(' '.join(partial) + ' ')
if line == groups:
lines.append(" " * pad + "-" * (num_categories * 2 + 4))
if line > groups:
partial = [" " * pad]
for category in resume_categories:
if letter_index < len(category['name']):
partial.append(category['name'][letter_index] + ' ')
else:
partial.append(' ')
lines.append(' '.join(partial) + ' ')
letter_index += 1
print('\n'.join(lines))
def round_down(number, num_decimals=0):
multiplier = 10 ** num_decimals
return math.floor(number * multiplier) / multiplier
```
Failing test was writing that in it own - a bit convoluted way, it can be hard sometimes to figure out what it’s trying to say exactly.
You might have noticed, in the output, somewhere above what is on the image, printed None. That was actually what function was returning at the time. When function isn’t returning anything explicitly, python returns None.