Python Forum
Works first time, not second time, no code changes in between
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Works first time, not second time, no code changes in between
#1
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.

Attached Files

Thumbnail(s)
   
Reply
#2
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?).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fit straight line to pandas time series data with semilog plot schniefen 2 1,567 Mar-10-2023, 01:08 PM
Last Post: jefsummers
  Plot time series data schniefen 3 1,366 Mar-04-2023, 04:22 PM
Last Post: noisefloor
  Plotting Spectogram of a Time domain signal shuchi 2 1,307 Dec-01-2022, 09:15 PM
Last Post: jefsummers
  Simple animation -- time evolution of function schniefen 0 1,049 Nov-20-2022, 08:05 PM
Last Post: schniefen
  Calculate time taken to cool down a pool in python edwinostby 0 1,828 May-24-2021, 04:12 PM
Last Post: edwinostby
  Klein-Gordon equation in 2D (1 of space, 1 of time) tdub 0 2,122 Jan-06-2021, 04:56 PM
Last Post: tdub
  How to execute several fonctions at the same time yoyo 3 2,099 Dec-22-2020, 05:46 PM
Last Post: yoyo
  Time limit exceed problem chonchon430 10 3,674 Oct-06-2020, 11:39 PM
Last Post: deanhystad
  Python variable and function usage at a time prasanthbab1234 14 5,245 Sep-24-2020, 01:48 PM
Last Post: prasanthbab1234
  Simple time comparison superwave33 5 2,390 Apr-14-2020, 10:39 PM
Last Post: superwave33

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020