[python] How to correct "TypeError: 'NoneType' object is not subscriptable" in recursive function?