Clarify the last unit testing in the project "buget App" in the Scientific Computing with Python

Hello,

This is the subject of the requirement " Besides the Category class, create a function (outside 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."

Please I need your collaboration, I’m finishing the project “budget App” but in the last unit testing there is something confusing for me in the requirement specify “The height of each bar should be rounded down to the nearest 10” in this unit testing there are 3 values to show in the bar chart =
1.- self.food.withdraw(105.55) --> here is my confusion in the bar char is rounded down to 70 but I think the value must be rounded to 100 ???
2.- self.entertainment.withdraw(33.40) --> this is fine is rounded down to 30 in the bar chart
3.- self.business.withdraw(10.99) --> this is fine is rounded down to 10 in the bar chart

Thanks for your collaboration.

Have a nice day.

Fred.

Heya - the values are based on a percentage of total spend, not the actual dollar amount. :slight_smile:

Hello,

Thanks a lot for your reply.

I did the calculation manually and this was the result, I don’t know if I’m doing something wrong.

Total Spent = (food) 105.55 + (entertainment) 33.40 + (business) 10.99 = 149.94

Total Spent = 149.94

Percentage for (food) = (105.55/149.94)*100 = 70.394% --> in the bar chart is 70%

Percentage for (entertainment) = (33.40/149.94)*100 = 22.275% --> in the bar chart is 30%

Percentage for (business) = (10.99/149.94)*100 = 7.329% --> in the bar chart is 10%

Thanks again for your time and dedication.

Fred.

Which bar chart are you looking at? Your own or the test modules’? Why is food rounded down and the others rounded up? Computers aren’t inconsistent unless you specified to be like that. By default, rounding is done downward.

Try printing the test module’s string in shell. Notice the bottom number :slight_smile:.

1 Like

Thanks again for your reply, I’m looking in the bar charts in the unit testing :
this is the unit testing function in the " Budget App" is this :

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.')

When I print the value in the expected variable I got 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

Try printing it to a txt file and see the exact spacing. You can do this in a terminal by typing, (“sample” being your python file that prints strings)

sample.py > sample.txt

It’s basically asking you to break down the problem and solve each one then put it together again.

For example, this is a representation of a chart with x and y axis. How many characters must a y-axis label have for each line?

How would you iterate 100 to 0 in steps of 10’s and print it exactly to match the spacing?

How would you use the percentage calculated (it tells you to round down the number) and add “o” in each line? How many empty spaces does each line have? etc.

These practices,(although looks pointless at times) will help you with loops and comprehensions(if you use them, they are great).

Oh my god I was blind, I saw where is the problem, the Bar chart start with Zero. Everything is fine now, my previous calculation are fine.

In this project there are 11 unit testing, I need only to finish this unit testing to complete the project. there other 10 unit testing are working fine.

Thanks a lot again, you r message were helpful in the sense that helped me to analyze where was my mistake.

Thanks again.

Have a nice day.

Fred.

Well done on finishing this, I completed it this afternoon and yes getting that last test to pass was tricky. I had trouble with the spacing in the last line, ended up loading the code into Visual Studio so that I could see the output in a better format, I got there in the end but I felt your pain with this one.

Thanks for your message, I’m agree with you in your comment but with Visual Studio Code for everything is easier for debugging and developing.

Agreed, I almost always use VS Code. The Visual Studio tip is only for when you have an issue with the string formatting in the test cases, I found the VS debugger is especially useful for this.