Hey Tom,
If you want to debug your code you should try the python debugger pdb (it took me a while to start using it but it’s a game changer).
ipython -m pdb is your best friend in these cases.
Jupyter notebook (and many IDE’s) also support it pretty well. There are probably some good tutorials around…