Skip to content
Sign upLog in
← Back to Community
Am I missing something or is the unit test for this problem incorrect?
Profile icon
aprildevelops

I hate that it's come to this point in the process for me because the former of the two scenarios in my total is almost always true in situations like this, but here we are.

The code in question is the module level function create_spend_chart inside budget.py. I think I've implemented it correctly; here is the section describing what this function should do from the readme:

{
Besides the Category class, create a function (ouside of the class) called create_spend_chart that takes a list of categories as an argument. It should return a string that is a bar chart.

The chart should show the percentage spent in each category passed in to the function. The percentage spent should be calculated only with withdrawals and not with deposits. Down the left side of the chart should be labels 0 - 100. The "bars" in the bar chart should be made out of the "o" character. The height of each bar should be rounded down to the nearest 10. The horizontal line below the bars should go two spaces past the final bar. Each category name should be vertacally below the bar. There should be a title at the top that says "Percentage spent by category".

This function will be tested with up to four categories.

Look at the example output below very closely and make sure the spacing of the output matches the example exactly.

Percentage spent by category 100| 90| 80| 70| 60| o 50| o 40| o 30| o 20| o o 10| o o o 0| o o o ---------- F C A o l u o o t d t o h i n g

}
As I said I'm pretty sure my code will fulfil these requirements, but when I run the unit test for this function my code produces the following chart:

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

while the unit test is expecting this:

Percentage spent by category 100| 90| 80| 70| o 60| o 50| o 40| o 30| o 20| o o 10| o 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

so I have the formatting and labeling down, but the actual "bars" of the chart are supposedly incorrect. Here's the code in the unit test that informs the "size" of the "bars" on the resulting chart:

def test_create_spend_chart(self): 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) 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 it looks to me like my code is producing the correct chart, and that the hard-coded "expected" chart-string provided by the designers of the problem is incorrect. If I'm missing something please let me know!

I know my code is probably quite bad since I'm a beginner, please excuse any inefficiency or stylistic deficits!

Voters
Profile icon
aprildevelops
Profile icon
DynamicSquid
Comments
hotnewtop
Profile icon
DynamicSquid

I think your code is correct, but you might be misunderstanding the problem.

Food has $900 deposited and $105.55 withdrawn. That would mean 11.72%, or roughly 10%, of the food money was spent, as the test shows.

Entertainment has $900 deposited and $33.40 withdraw, so that would be 3.67%, which is roughly 0%, just like the test shows.

Same for business.

Hope that helps :)

Profile icon
aprildevelops

@DynamicSquid
In my haste I accidentally swapped the expected vs actual charts, my bad! So my code produces the chart that shows ~0% 10%, 0% but the unit test is expecting the one that shows 0, 70, 20.

Profile icon
DynamicSquid

@aprildevelops
Ah okay, I see. Yeah I think the test is wrong because I can't seem to find any error with your code.