I am out of trial and error again Lists

Larry Hudson orgnut at yahoo.com
Sat Oct 25 02:04:00 EDT 2014


On 10/24/2014 09:37 AM, Seymore4Head wrote:
<snip>

> import string
Not needed, delete it.

> def nametonumber(name):
>      lst=[]
>      nx=[]
>      digit=[]
Not needed.  You create digit as an empty list, them immediately follow by assigning a string to 
it (NOT a _list_ of characters, but an actual string.)

>      digit="".join(str(i) for i in range(10))
digit is now the _string_ "0123456789".  Actually, a direct assignment here would be easier and 
shorter;
         digit = "0123456789"

>      for x in name:
>          lst.append(x)
lst is now a list of the characters in name.  That works, but you can do the same thing with:
        lst = list(name)

>      for y in (lst):
Parentheses not needed.

>          if y in lst(range(1,10)):
This is the line the traceback is referring to.  lst is a list, but the parentheses tell Python 
you are trying to call it as a function.  That is what the "TypeError: 'list' object is not 
callable" means.  Also it seems you are now trying to make a list of the digits 1-9 without the 
zero.  Is this what you want?  I don't think so, but if that IS what you want you can do it 
easier with slicing (look it up, VERY useful):  (Remember, digit is already a string of the ten 
digits, with zero as the first character.)
         if y in digit[1:]:
Otherwise if you do want zero included you simply need:
         if y in digit:

>          #if y in "1234567890":
>          #if y.isdigit():
>          #if y in digit:
All of those should work, with the zero.

>          #if y in string.digits:
Ok (I think?), but the string module is effectively obsolete -- most of its capabilities have 
been replaced with newer, better string methods.  It is RARELY useful.  I have never used it 
myself so don't know too much about it.  It is definitely not needed here, and is why I 
suggested deleting the import.

>              nx.append(y)
 > ...

      -=- Larry -=-




More information about the Python-list mailing list