[Tutor] printing values from one list that correspond to that inanother list

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Jun 6 18:42:21 EDT 2004


> def convertxt():
>     if original in alphabet[:26]:

This is fine (although using the string module constants is 
probably still a good idea!) because you are returning a list 
then checking to see if original is in that list

>         print binary[:26]

But this isn't because you are again generating a list and 
printing all of it. You need to find the index of your 
character within the list and then use that to index binary:

if original in alphabet[:26]:
    print binary[alphabet.index(original)]

When stuck it's worth reading the documentation carefully to 
check what types each action expects and returns. Also to 
browse the list of available functions and constants.

There is a saying that "Python comes with batteries included"
which means that usually you can find something in the library 
that will do the bulk of your job for you.

The pyhon >>> prompt is your friend here because it makes it 
very easy to try ideas before committing them to a module/program.

Of course sometimes we just want to experiment and do things for 
the sake of finding out how. That's OK too. :-)

HTH,

Alan G.



More information about the Tutor mailing list