While and If messing up my program?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Oct 5 14:28:34 CEST 2005

On Wed, 05 Oct 2005 00:10:12 -0700, ajikoe at gmail.com wrote:

> hi,
> to get howmany element list appear you can code:
>     ttllst=[4,3,45,3]
>     for x in ttllst:
>         print x, ttllst.count(x)
>     pass
> to get non duplicate element list you can code:
>     ttllst=[4,3,45,3] 
>     print list(set(ttllst))

These are excellent things to use in Python, but for learning programming
skills, they are terrible because they rely on black boxes to do

I remember once having to code a matrix division algorithm in "the
language of your choice" for a comp sci course. So of course, being a
smart arse, I programmed it in the HP-28S calculator programming language,
which just happened to have matrices as a native object type, complete
with division.

I answered the question exactly, and learnt absolutely nothing from the
exercise. (For the record, I failed that course.)

It is good to tell the original poster how he should be implementing the
code he is trying to write, but that is not as important as helping him
work out where the code is going wrong.

In this case, the problem is that C.J. carefully checks that his indexes
are within the valid range for the list, but (s)he makes that check
*after* attempting to use the indexes. So the code fails before it reaches
the test.


More information about the Python-list mailing list