Budget App - Scientific Computing with Python

I’m running into something really weird here, and would love your opinion on what’s going on. This is my replit repo
I’m passing all the project tests on replit, except the one that requires you to print out the chart of categories. Visual aspect isn’t exactly the problem, as the charts are visually exactly the same, the problem as far as i’m aware lies in the spaces and specifically the lack of spaces in my project.

Below you can see comparison between the expected replit output, and my output.
image

As you can see, whenever there’s no characters they fill the width with spaces.

In my code, i took approach of doing an if/else. Everything works correctly except the spaces.
The weird thing is that length of the string gets longer after else statements by exactly 3 (as if spaces were added), but the spaces don’t appear in the output.

I added a comment “I believe problem is here”, use it to find the code below.
The code in red brackets should be adding 3 spaces when if statement fails. And judging by the length of string it seems to be adding it just fine… but the spaces aren’t there?

image

The even weirder thing is, once you change that ’ ’ to ‘X’ or ‘/’ or really any other character it works fine.
image

Anyone could shed some light onto this?

I vaguely recall the output in replit console was always a bit weird regarding showing the spacing. As far as I can tell they are being added.

Remember this function is not supposed to print anything on it own, but it’s expected to return the string with chart.

your function prints and doesn’t return, it should return

also you have a

def create_spend_chart(categories):
  pass

at the end, maybe yo should not have twice the definition of a function

@sanity @ILM Thank you! I was debugging all the time with print statements, my assumption was that print statements and return are going to give me the exact same output in console, but it seems like that’s not the case with spaces?

I managed to fix the code real quick and passed all tests:
image

This makes me curious, with more complex code how would someone debug if spaces are appearing correctly if return doesn’t give any output for user to check in console and print isn’t reliable with spaces?
Is there some best practice way to go about this?

as a rule of thumb a function should always return something, you can print a variable before returning it, but print does nothing to build the returned value of the function

if you want to check that there aren’t spaces around a certain value, you can print an f string interpolating that value, print(f'"{var}"'). This can show the difference between "wolf" and "wolf "

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.