Flummoxed by Ex 41

I have copied the code for ex 41, checked, checked and checked it. But it does nothing! I have copied and pasted your raw code for it, and it magically works!!

When I try to run my code, it says I have an indentation error on line 54. I have checked this SOOOOOOOOOO many times! If I un-indent it, I don’t get an error message, but still, when I try to run the program, nothing happens!

Any suggestions would be appreciated.



Hello @Papajojo289

There is some things you can do to perhaps find the reason your self.

  • leave it for a day.
  • write a mail to yourself (Zed’s tip)
  • have a look at the lines above what the Traceback tells.
  • Put up your code in this thread between [code] and [/code] . Then other can see your code and help you how to do.
    -Sometimes you find it yourself when its uploaded.

Good luck.

Welcome to the forum, Papajojo289.

If you’re getting an indentation error, what are you using to write your code? If you’re using any sort of proper text editor, there should be an option to render whitespace. It might be called a number of things, but regardless of the name, it will show some sort of symbol wherever you have whitespace, be it a regular space or a hard TAB character or other whitespace. I find that if I somehow make an indentation mistake when writing Python, this feature makes it really easy to find and then correct.

1 Like

Thanks. I’m not really sure what you mean by ‘whitespace’? I’ve tried writing the code using Mu, and also Notepad and running it in a command window, but whatever I use, I an indentation error where the code says "#fake class names/ other names/ param lists. If I unindent them, the indentation error disappears, but the program doesn’t run!

This is one of the frustrating things about python, but the kind of spacing in front of each line matters. Take a look at this:

def test(x):
    for i in x:
    def wrongspaces():
      # does some stuff

The error you’re most likely hitting is this one, where you think you’ve placed def wrongspaces at column 0 lined up with def test, but in reality you accidentally indented it so it’s inside def test(). When that happens it won’t run because it’s considered a part of test().

What’s worse is probably all of the code inside def wrongspaces(): is also indented, so you have to “de-dent” to bring it all back to the left by the same amount.

To do that depends on the editor you’re using (tell me and I’ll get specific) but usually it’s something like select all the text and hit shift-tab to move it back one block.

“Whitespace” just means a blank space, tab, or similar character. It’s called whitespace because for thousands of years paper was white and if you put space then that space is white. Really it should be called “blank space” given that you can have a wild array of backgrounds. Like, my text editor is dark brown.

Yes, this is a good point. I didn’t think about the fact that you might not be familiar with this term, @Papajojo289 . My bad. “Blank space” is a much more useful term.

A free service run by Zed A. Shaw for learncodethehardway.org.