Beginners question.

From: Player (asif_at_go-away-spammer.com)
Date: 08/31/04


Date: Tue, 31 Aug 2004 18:23:23 +0100

Hi all

 I am teaching myself python and so am following a few tutorials from
online.

In one of the tutorials an exercise is the following...

 As an exercise, write a loop that traverses the previous list and prints
the length of each element.

Firstly I just wanted to see if I could get the length out of those elements
that were list type.
I no thats not exactly what the exercise asks for, but I thought I would
start there.
However I am stuck and cant figure out why my code is doing what it's doing.
My code is as follows....

mylist = [1, 2, ["three", "four"], "five"]
for item in mylist:
    if item == type(list):
        myitem = len(item)
        print myitem
    else:
         print item

All my code seems to do is print out each element of the list on a new line.
What I had expected it to do was this....
1
2
2
'five'

but it doesn't, what it does do is this...

1
2
['three', 'four']
'five'

I expected the line,
myitem = len(item)
 to issue the variable, myitem, with a value that was the length of item.
But as you cans ee it doesn't and simply ignores all that code and does the
else:
    print item
part instead, misisng out the first half of the for loop.

Were am I going wrong ??

Thanks in advance for any help :)

M.B aka - Player -

-- 
*************
The Imagination may be compared to Adam's dream-
he awoke and found it truth.
John Keats.
************* 


Relevant Pages

  • Re: Beginners Question on an exercise from Grahams ACL
    ... As another Lisp newbie going through the ACL book I will lend some ... just been taught and to exercise my brain on them - not with some other ... I find it very hard to follow tutorials due to the desire ... three of the Gigamonkeys online book today and rather enjoyed working ...
    (comp.lang.lisp)
  • Re: struts using jdbc
    ... Currently I am working on an exercise where I ... In your Action class create a data access object of this class or one of the package types, Use that instance to execute your data operations. ... There are JDBC tutorials and other documentation amply available on the web. ...
    (comp.lang.java.programmer)
  • Re: [video] Tutorial remake: 645
    ... crossing throws and a single straight throw in 534 whereas in 645, ... exercise for learning 645 then? ... balls which don't 6 do) ... Especially when it comes to tutorials, ...
    (rec.juggling)
  • Esperanto-type courses for Spanish.
    ... offered full-out email correspondence classes with a personal tutor and ... anything to exercise what the tutorials give me. ...
    (sci.lang)
  • Re: modified "histogram" exercise from K&R2
    ... K&R2 exercise 1-13. ... Here's your printing loop, ... OK, a histogram is like a graph with x and y axis, like your solution ... part of it after you gave me LONG and CLEAR explanation but i can't ...
    (comp.lang.c)