Python Forum

Full Version: Works first time, not second time, no code changes in between
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi! Python beginner here.
In the screenshot, you can see me execute the same command twice in a row. The first time it works like it's supposed to, but the very next time I execute it (with no code between!!), I get the error message shown in the screenshot.

Any tips or knowledge as to where I should look in the code? (Also, what is the significance of the error saying "string index"?)

Thank you so much.
It is not uncommon for jupyter notebook code to run differently each time it is executed. Jupyter notebook is kind of like a spreadsheet in that calculations in a celll may affect calculations in other cells. This makes it a powerful tool for doing what-if type analysis, but can make it more difficult to debug than a Python program which will run the same way every time.

I don't understand what name[547] is supposed to do. Please explain.

The significance is that name probably isn't a str the first time cell 71 is run. In cell 71 you use "name" to iterate through a list (I think) of str objects. Now "name" is a str, and a short str at that (maybe "HR 5666?).