Budget app - spend chart

Hi again, so I took a break from the budget app and focussed on the other 2 remaining projects (and finished them :)) and now returned again. I managed to solve a lot, but I don’t understand this problem with the spend chart. Can anyone help me in the right direction?

The spent chart shows an AssertionError and when reviewing it, it seems that when generating the histogram something happens with the 3rd line of letters (s o t), but I cannot find what it is exactly (nor do I understand what is different between what is generated (with ‘-’) and what is expected (with ‘+’).
I tried to reproduce the error in Python Tutor, but there everything works fine there.

Link to code: https://repl.it/@Brain150/FCC-boilerplate-budget-app#budget.py

the part of the code that generates the names vertically is from line 100 - 109 (see below)

longest_cat = len(max(cat_names, key = len))
	
#	split category titles in letters and collect
for digit in range(longest_cat) :
	vertical_cat = "    "
	for name in cat_names :
		try :
			vertical_cat += " " + str(name)[digit] + " "
		except IndexError:
		 	vertical_cat += "   "

Look at the example chart which is printed above the error. It only has one line, the same issue is with the tested one with s o t letters. I’m assuming it’s marked like that in the test output, because that line actually matches the expected output at some point, but it has missing lines before and missing lines after.

Try to figure out why only single line is printed under the chart.

Thank you, good hint to look at the example above the error. Unfortunately (for me) it makes it more complicated: In that print the 2nd letter of each category is printed (fOod, cLothing, aUto, ) and in the spend chart in the error the 3rd letter is printed.
When running the code, it iterates and returns all letters :confused:

1 Like

For now ignore fact that one prints 2nd letter and one 3rd (specific reason will come into play later, but it’s not necessary yet), try to pinpoint place that’s responsible for preparing these category names and check whether isn’t there some problem.

What do you mean with last sentence that it returns all letters?

1 Like

Well…that was good help! I found where it went wrong: 3 lists with all characters and comma’s in between of each category were formed instead of a list with the 3 categories as strings. Solved that part now :slight_smile: and experimented with returning the parts of the histogram vs not returning the parts, but constructing it at the end, which seems to give a result that looks like what it should be (except for the names of the categories).

Now, the += does not do what I would want it to do: I found the function works, iterating through all elements fine, but in stead of adding each new character to vertical_cat, it replaces it, resulting in just the last line being returned. As far as I could find out(which is why it took me so long to answer), += can be used with strings (which all parts of this formula are), so that should not cause a problem. I changed the code and now it does work (at least it returns the categories vertically and in the right position), but still fails : "Expected different chart representation. Check that all spacing is correct. ". I cannot find the differences between the lines with - and + in the error. I added 11 spces to match the requested amount of characters, but (as expected) this solves not the spacing problem.
Do you have any pointers for me again?

	vertical_cat = ""
	for digit in range(longest_cat) :
			vertical_cat += "    "
			for name in cat_names :
					try :
							vertical_cat += " " + name[digit] + " "
					except IndexError:
							vertical_cat += "   "
			vertical_cat += "\n"
	vertical_cat += "           " # added to match the amount of characters needed
1 Like

It can be hard to figure that out in repl.it console, as seemingly there are spaces everywhere there. This should make it easier:

      ----------
-      B  F  E 
+      B  F  E  
?              +
-      u  o  n 
+      u  o  n  
?              +
-      s  o  t 
+      s  o  t  
?              +
-      i  d  e 
+      i  d  e  
?              +
-      n     r 
+      n     r  
?              +
-      e     t 
+      e     t  
?              +
-      s     a 
+      s     a  
?              +
-      s     i 
+      s     i  
?              +
-            n 
+            n  
?              +
-            m 
+            m  
?              +
-            e 
+            e  
?              +
-            n 
+            n  
?              +
-            t 
?              ^
+            t  ?              ^
-             
1 Like

Thank you so much! Now it works. Fiddling with the spaces (which results in a reduced number of “loose spaces” in the code, so improved readibility, and add an .rstrip("\n"), so the final line is correct too.

With this my first Pyhthon certificate is complete, so I am very happy :smiley:

vertical_cat = ""
	for digit in range(longest_cat) :
		vertical_cat += "     "
		for name in cat_names :
			try :
				vertical_cat += name[digit] + "  "
			except IndexError:
					vertical_cat += "   "
		vertical_cat += "\n"

chart = title + histogram + dash_line +"\n" + vertical_cat.rstrip("\n")

For me to learn looking into the bigger picture too:

What kind of tool did you use wiht the red and green (and clear spaces)?
How did you find out so fast where to look what went wrong with my first question? (I discovered it after you pointing out and letting Pyton Tutor suffer until it smoked;))

that’s just how a block of code with + and - at the start of lines is visualised on the forum
it’s useful to see the spaces tho

1 Like

Yeah, greens and reds are from forums.

There are probably few reasons why I quickly found out where to look at. I’m familiar with this project having done it myself, so from the start I’m having some idea what is where and what to check. I most likely also in the past made similar mistakes with loops and rewriting variables that shouldn’t be rewritten.

Generally with time this should get easier, especially when you really find what is wrong on your own, after just reading small hint.