I have rewritten this method a couple times, trying to be more efficient and using less code(I tend to write books of codes:D) and still I can’t get through the test.
Here is my code, maybe I am just blind at the moment
def create_spend_chart(cat):
end_string = "Percentage spent by category\n"
get_percentage_ordered = dict()
categories = []
#getting the percentage for every category
for i in range(len(cat)):
temporary_max = float()
temporary_minus = float()
counter = 0
for amounts, descriptions in cat[i].ledger:
if cat[i].ledger[counter][amounts] >= 0:
temporary_max += float(cat[i].ledger[counter][amounts])
counter += 1
else:
temporary_minus += float(cat[i].ledger[counter][amounts])
counter += 1
#I did this just in case, for example 50.99999 can become 60.00 like this, maybe my error was laying there
get_percentage = 100 / temporary_max * -temporary_minus
get_percentage = "% .2f" % get_percentage
get_percentage_ordered[cat[i].category] = float(get_percentage)
#make the list with the categories, it will be useful later
for k, v in get_percentage_ordered.items():
categories.append(k)
#to get the bar chart done, my error seems to lay here and it looks like I actually fail to print the circles at all
percent = 100
bars_to_add = str()
while percent >= 0:
bars_to_add = str()
for i in range(len(categories)):
if get_percentage_ordered[categories[i]] >= percent:
bars_to_add += " o "
else:
bars_to_add += " "
end_string += f"{percent}".rjust(3, " ")+"|"+bars_to_add+" \n"
percent -= 10
end_string += " "
for i in range(len(cat)):
end_string += "-" * len(cat)
end_string += "-\n"
cat_string = str()
#index for letter
index_l = 0
#index for category name
index_c = 0
for i in range(len(max(categories, key=len))):
cat_string += " "
index_c = 0
for ii in categories:
try:
cat_string += categories[index_c][index_l]+" "
except:
cat_string += " "
index_c += 1
index_l += 1
cat_string += "\n"
end_string += cat_string
#this made me get rid of one error, so i keep it
return end_string.rstrip("\n")
to repl
Just in case my repl link.
Thank you very much in advance
Okay, I just cant stop looking for the error, so when I run the create_spend_category on my computer with the values from the test method i get this result:
(It just got formatted weird, posting it here) #i just printed that out, to see if the percentages are actually correct
{‘Business’: 1.22, ‘Food’: 11.73, ‘Entertainment’: 3.71} #------------------------------------------------------------
Percentage spent by category
100|
90|
80|
70|
60|
50|
40|
30|
20|
10| o
0| o o o
----------
B F E
u o n
s o t
i d e
n r
e t
s a
s i
n
m
e
n
t
[Finished in 2.4s]
def test_create_spend_chart(self):
#I used these values
self.food.deposit(900, "deposit")
self.entertainment.deposit(900, "deposit")
self.business.deposit(900, "deposit")
self.food.withdraw(105.55)
self.entertainment.withdraw(33.40)
self.business.withdraw(10.99)
#I used these values ^^^^^
actual = create_spend_chart([self.business, self.food, self.entertainment])
expected = "Percentage spent by category\n100| \n 90| \n 80| \n 70| o \n 60| o \n 50| o \n 40| o \n 30| o \n 20| o o \n 10| o o \n 0| o o o \n ----------\n B F E \n u o n \n s o t \n i d e \n n r \n e t \n s a \n s i \n n \n m \n e \n n \n t "
self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
So I can see that there are supposed to be more than four circles in the output, but I just get four. Are they using also different values? I also added the values from the main.py, but then I just get 3 circles.
The problem is the percent calculation. It looks like you are calculating the percent spent in each category:
while the project wants you to calculate the percent of the total spent in each category. In other words, if you spent a $100 total and $25 was food, $60 was business, and $15 was entertainment, your percents would be 25%, 60%, and 15% respectively, regardless of how much was in the budget for each category.
Oh God!
I was sitting so long in fron of the computer trying to figure out on how to solve this and then my mistake was reading the task.
Thank you a lot!!!