python - Syntax Error when using except ValueError? -


group = range(1, 1001)  num in group:     num_length = len(str(num))     in range(num_length):         if str(num)[i] == '1' or str(num)[i] == '7':             group.remove(num)         except valueerror:             pass         else:             pass 

i trying remove numbers contain 1 or 7 digits in them. avoid "valueerror: list.remove(x): x not in list", added "except valueerror: pass". however, have "syntaxerror: invalid syntax".

first, range objects immutable, means cannot remove element range object. moreover, possible remove elements sequence iterating over.

second, except block must related try block containing code may generated error caught except.

what suggest create new list elements want keep (that without 1 , 7 in it). can simplify check inclusion of 1 , 7 using in operator.

group = range(1, 1001) data = []  num in group:     s = str(num)     if not ('1' in s or '7' in s):         data.append(num)  print(data) 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -