Help with Exercise14

Can someone please help me figure out why I keep getting an error?

I’ve written this exactly like in the book. I’ve even made other attempts around this but I can’t figure out why this won’t work.

Is it something to do with Zed running 3.6 and me running 3.8?

Any feedback would be great. Thanks.

Line 8, are you missing a closing bracket? )

When you have an error it’s an approximation of where the problem could be. It can be on that line, or any of the lines above it. In this case, your line is fine, but above it the print line does not have an ending ) right-parenthesis.

Now, I’m going to give a suggestion to help you avoid this in the future. I want you to delete this file, and code it by writing only 1-3 lines before you attempt to run it. “Lines” doesn’t mean text lines, but more lines of code. So see the giant print at the end that takes up 5 lines? That’s one “line”. Otherwise, you’d type lines 1, run it, lines 2-3, run it, line 4, run it, line 5-6, run it, and so on until you’re done.

Why do this? See how the Python interpreter is telling you where an error is, but it’s not accurate? You can use Python to confirm that what you think you wrote actually works by just running it. Then if you type 1-2 lines of code and it has a problem, you know it’s in those 1-2 lines of code you just typed, not somewhere randomly within the entire file.

Also, this incremental building of code is how all successful programmers code. We all learn eventually to not trust our own brains and rely on tools to to keep us straight. In this example, you swore it was exactly the the same, but your brain just glossed right over the mistake because it considers it unimportant. This isn’t your fault at all, it just a thing all brains do in an attempt to help you deal with information, but in this case isn’t really helping.

For example, did you see how I wrote “to to” and “the the” above? If you missed those then that’s another example of your brain skipping unimportant details, which is fine in English, but in programming a tiny little thing missing can cause huge problems, so this isn’t helping you at all.

Rather than trust your brain (which is wrong, a lot, even though you don’t know it), you learn to use Python to confirm your code was written well enough to run.

1 Like

That makes a ton of sense. I for one, have been in the habit(over the course of a year trying and failing to learn to code) doing “code along” courses which do not even mention coding this way. I wind up typing huge swaths of code, and the mistakes are buried in there somewhere. I’m trying to break that habit now.

1 Like

I’m not sure how I got into the habit of constantly running my code, but another thing that might help you do that is to try doing automated testing. Doesn’t matter if you do it before or after you write the code, but just always write a test for code you write, and run your tests all the time. There’s even programs that will run them for you and pop up alerts with the results.

Thank you so much. The help and advice you provided was great. I appreciate you taking the time to provide a very thorough response.

Great, try that “do it twice for luck” practice and also incremental code building and then let me know how it’s working for you.