This question might be unambiguous, but it would be of great help if you take time to answer it | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

This question might be unambiguous, but it would be of great help if you take time to answer it

Is it possible that my code runs till a number without any error, but then gives an error called " 'NoneType' object is not subscriptable ". How is this possible, and what should I do to remove the error. I cannot share the code here because I have to maintain secrecy at the work place.

24th Jun 2018, 6:13 AM
Anshul Aggarwal
Anshul Aggarwal - avatar
9 Answers
+ 8
Perhaps you are iterating through a collection with a loop or something and at some point you don't find any value, thus it is returning None. And you of course can not subscript the None object. What you can do is envelop this part of code which contains the subscription with a try/except clause: try: <the subscription part> except TypeError: <what the code should do in such case> EDIT: [OFF-TOPIC] omg, Nikhil Dhama is back :)
24th Jun 2018, 6:24 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
def delete_number(x,list2,length): for i in range(length): if list2[i]==x: list2.pop(i) return list2 else: continue use this definition it will work fine, i guess here 'length' is the length of the list2, this argument is ambiguous you can find the length of list inside of the function itself. def delete_number(x,list2): for i in range(len(list2)): if list2[i]==x: list2.pop(i) return list2 else: continue now, point is why are you getting that error? it's because list has indexes from 0 to it's (length-1), and in your function you are using range(length + 1) which will return values from 0 to length(included) and list2[length] is not defined, but this is a indexerror! and there is no need to use if (i<= length) (it's a redundant condition) , it's for sure if your are using range(length), then it will return value from i = 0 to i = length-1 -1 I hope this solves your problem.
24th Jun 2018, 6:59 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 1
Anshul Aggarwal check the part of your code where you are calling that function, as 李立威 mentioned, check the list you are passing to the function, is it really a list?, check that part.
24th Jun 2018, 7:32 AM
Nikhil Dhama
Nikhil Dhama - avatar
0
https://code.sololearn.com/c0cmkryA2jE4/?ref=app This is the function which is bothering me with the error, please see if you could help me out.
24th Jun 2018, 6:46 AM
Anshul Aggarwal
Anshul Aggarwal - avatar
0
After seeing your example code, I thought that the problem may arise from "list2". I guess that maybe list2 is something that is/should be returned from another function, but here since it says "list2 is of NoneType", so I guess the problem may be: <list2 is returned wrongly from another function>, such as def f() : pass #forget the return statement list2 = f() #list2 is of NoneType Edit: If list2 is truly a list type object, and that the variable "x" has been declared, your code: (list2[i] == x) can only cause an IndexError, not a TypeError, so I think that the problem results from "list2" itself
24th Jun 2018, 7:18 AM
李立威
0
I know I have already performed the operation, as range(Len(list2)), the error then I encounter is as "a none type object has no length". Please suggest me what to do next.
24th Jun 2018, 7:28 AM
Anshul Aggarwal
Anshul Aggarwal - avatar
0
I may need to know an example argument that you pass into "list2 parameter of the function" to get more information.
24th Jun 2018, 7:33 AM
李立威
0
yes the one passed to the list 2, is a list and if it wouldn't be a list then how would my function run perfectly without any error till 500 integers.
24th Jun 2018, 8:06 AM
Anshul Aggarwal
Anshul Aggarwal - avatar
0
Check somewhere in between it doesn't fetching a list, as Kuba mentioned earlier use exception handling try : # do whatever you are doing except TypeError: # what you want to do otherwise. where 'list' is the list you are passing to the function.
25th Jun 2018, 9:20 AM
Nikhil Dhama
Nikhil Dhama - avatar