[Tutor] printing multiple values from a list in one command

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Jun 6 12:56:26 EDT 2004

 > etc). However, when I try to write the code such that when the text
to be
> translated is in the lowercase (the first half of ascii_letters),
the program
> should print the corresponding value in binary.

> Short of defining each letter as a binary number,

You mean like ASCII?
I'm not sure what you are trying to achieve by mapping one binary
(the ASCII code) to another (your arbitrary value)?

>         if char in alphabet[range(0-26)]:

This won't work because range returns a list and a list index must
be a single integer. I suspect you just want

if char in string.ascii_lowercase:
   print binary[ord(char)]

>         elif char in alphabet[range(27-52)]:
>             print binary[range(27-52)]

if char in string.ascii_uppercase:
   print binary(ord(char))

Or replacing both

if char in string.letters:
   print binary(ord(char))

>         elif char not in alphabet:
>             print "Sorry, you didn't enter valid text. Please enter
> letters only."

And this should probably be plain 'else' just to catch any other

If you really do want to use your handcrafted alphabet you should use

if char in alphabet[0:26]:  # or just [:26]

elif char in alphabet[16:52]: # or just [26:]

BTW, It has been discussed several times in the past on tutor, how to
write a function to generate binary strings from numbewrs, you might
find a search of the tutor archives onActivestate useful. Also for
hex display the standard format string can do that for you (%x or %X)

Good luck,

Alan G.

More information about the Tutor mailing list