Struggle Sesh: VS Studio & Python

Last night I spent a ridiculous amount of time coding Python within my VS Studio work environment. The mistake I was making was SO SIMPLE and had nothing to do with my code at all! I’ll look back on this and laugh one day, I’m sure. For now, I’ll outline it for my fellow noobs who might also make this mistake, especially if you’re used to coding within a Jupyter notebook and learning via interactive notebooks, like on Kaggle.

Issue: I kept getting an error message when trying to run the code block, specifically that one of my variables was not defined:

You might be thinking, well, Shelby, you probably just didn’t define your variable “result”! I promise, I reviewed the code, and all variables were neatly defined. I tried again, even cleared the whole code block, retyped it, simplified the code…nothing was working. Stuck, I asked ChatGPT for help, and it said my code was correct and didn’t know why it wasn’t running, hah! My face at that moment:

So, I took a screenshot of my code block, including my interactive view and the full VS Studio workspace and shared that with ChatGPT – it quickly had the answer – I wasn’t selecting ALL CELLS before running them. Whoops. At least my code was correct, right?? I selected all and then ran the code, and it worked!

Why did this happen?

Almost all of the Python courses I’ve taken use either a Jupyter notebook to practice or a proprietary version of something similar to the Jupyter format. When I would run the code, I would use SHIFT – ENTER to run my cells of code for each block and create the output one line at a time, because when you’re taking a course on most platforms, you’re learning in small code blocks and always run the code for each block before moving on to the next section.

Essentially, I was used to running code as I went along, and any variables or functions were saved to the global environment, meaning, it “remembered” everything I coded above so I could continue with everything working correctly as I continued below. There’s probably a better way to explain this, but I don’t know it yet. When I used VS Studio and built out my big block of code all at once (“big” is relative, it was just about 20 lines of code, lol), but wasn’t running it in sections (after my function definitions, for example), the later code wasn’t set up for success to run properly.

So, there you have it. A simple coding mistake I won’t forget now. Ah, learning.

Leave a comment/share your thoughts (please be kind)