On Wed, 22 Oct 2014 22:43:14 +0000 (UTC), Denis McMahon
<[email protected]> wrote:
>On Wed, 22 Oct 2014 16:30:37 -0400, Seymore4Head wrote:
>
>> def nametonumber(name):
>> lst=[""]
>> for x,y in enumerate (name):
>> lst=lst.append(y)
>> print (lst)
>> return (lst)
>> a=["1-800-getcharter"]
>> print (nametonumber(a))#18004382427837
>>
>>
>> The syntax for when to use a () and when to use [] still throws me a
>> curve.
>>
>> For now, I am trying to end up with a list that has each character in
>> "a" as a single item.
>>
>> I get:
>> None None
>
>First of all, an empty list is created with:
>
>emptylist = []
>
>whereas
>
>x = [""]
>
>creates a list containing one element, that element being an empty
>string. Not the same thing!
>
>Did you try stepping through your code line by line in the interpreter to
>see what happened at each step?
>
>note that append is a method of a list object, it has no return value,
>the original list is modified in place.
>
>>>> l = ["a","b","c"] # declare a list
>>>> l.append( "d" ) # use the append method
>>>> l # show the list
>['a', 'b', 'c', 'd']
>
>So your line:
>
>lst = lst.append(y)
>
>should be:
>
>lst.append(y)
>
>Finally, did you really intend to pass a single element list into the
>function, or did you intend to pass a string into the function?
>
Those string errors were desperate attempts to fix the "append" error
I didn't understand.
>There is a difference between:
>
>a=["1-800-getcharter"]
>
>which creates a single element list, the one element is the string "1-800-
>getcharter", and:
>
>a="1-800-getcharter"
>
>which creates a string variable with the value "1-800-getcharter"
>
>when you pass a list containing a string to your function, enumerate will
>look at each list element, so if your list contains one string, enumerate
>will return the pair 0, the_string, so the string gets appended to your
>empty list as a single item.
>
>The code I think you wanted to write is as follows:
>
>def nametonumber(name):
> lst=[]
> for x,y in enumerate(name):
> lst.append(y)
> return lst
>
>a="1-800-getcharter"
>print ( nametonumber(a) )
>
>I suggests that you study very carefully the differences between this and
>your original code until you understand the reason and effect of every
>difference, as only by doing so will you discover the misconceptions
>which you seem to be operating under, and until you can get some of those
>straightened out, you're not going to make a lot of progress.
>
>Try running the original code and my suggested alternative line by line
>in the interpreter, and examining the state of relevant variables after
>each line of execution.
>
>Here's a code file with both your original code and my modified code with
>comprehensive print statements inserted for debugging. By referencing the
>debugging statements back to the code, you should be able to determine
>exactly where in your original code the value of "none" comes from.
>
>### code starts
>
>print ( "original code" )
>
>def nametonumber(name):
> print ("a) name =", name)
> lst=[""]
> print ( "b) lst = ", lst )
> for x,y in enumerate (name):
> print ( "c) x = ", x, "; y = ", y, "; lst = ", lst )
> lst=lst.append(y)
> print ( "d) lst = ", lst )
> print (lst)
> return (lst)
>
>a=["1-800-getcharter"]
>print ( "e) a = ", a )
>print (nametonumber(a))
>
>print ( "modified code" )
>
>def nametonumber2(name):
> print ("f) name =", name)
> lst=[]
> print ( "g) lst = ", lst )
> for x,y in enumerate(name):
> print ( "h) x = ", x, "; y = ", y, "; lst = ", lst )
> lst.append(y)
> print ( "i) lst = ", lst )
> return lst
>
>a="1-800-getcharter"
>print ( "j) a = ", a )
>print ( nametonumber2(a) )
>
>### code ends
>
>If you run the above code exactly as it is, you should see in the output
>how the enumeration reacts according to the different data it is given to
>enumerate, and also where lst is assigned the value none.
>
>As I said above, getting your head round why this is happening is
>essential, and I really do suggest that you slow down and try and
>understand these basic concepts, because at the moment it seems you are
>striving to attempt more and more complicated things without
>understanding the basics upon which they are constructed, and like many
>other similar newsgroups, it's been my experience in the past that there
>is limited tolerance here for people who repeatedly make the same basic
>errors without learning from them.
Thanks a lot for all your suggestions. I haven't learned to use the
interpreter yet. I do plan on learning to use it.
The problem with that at the moment is that I have enrolled in an
online computer class. They use Codeskulptor.
Codeskulptor code is not compatible to standard Python. When I finish
the class I do plan on using Python 3 and will learn the Python 3
stuff.
Codeskulptor has a Viz mode. I have tried using it, but so far it is
still pretty confusing. I will try to use it more.
I have saved your message and will crack the interpreter on it soon.
My trouble is trying to search for practice problems that are not too
hard yet.
Thanks again
--
https://mail.python.org/mailman/listinfo/python-list