python - Why is the error handling not working for None input? -


def copy_list(t): try:     if type(t) list:         t_copy=[]         n=len(t)         i=0         while i<n:             t_copy.append(t[i])             i+=1         return t_copy except typeerror:         return "not list" 

the problem says should write function takes list of integers input , returns copy of it. should raise exception if input not list. unable understand why code unable raise exception if value not of list type or when input none?

the try/except block used gracefully handling exceptions thrown interpreter when unexpected or illegal value encountered, not raising exceptions intentionally. want raise keyword. see question: how use "raise" keyword in python

as suggestion, code this:

def copy_list(t):     if isinstance(t, list):         t_copy=[]         n=len(t)         i=0         while i<n:             t_copy.append(t[i])             i+=1         return t_copy     else:         raise exception('not list') 

edit: think you're going want isinstance function, , have edited code accordingly. info on can found here.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -