[Tutor] String to integer?

Chad Crabtree flaxeater at yahoo.com
Wed Sep 1 20:32:53 CEST 2004


Blake Winton wrote:

> adam wrote:
>
>> What's the easiest way of creating an integer which is based on a 
>> value of
>> a letter? I really don't mind if it was based on the ascii code,
or
>> another equivalent.
>
>
> >>> ord('a')
> 97
> >>> chr(97)
> 'a'
> >>> ord('ab')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: ord() expected a character, but string of length 2 found
>
> (Putting on my user-hat for a second, you _really_ want 26 buttons
on 
> a single form?!?  That's probably going to be confusing to look at.
 
> Is there another way you could get the user's input?)
>
Well how about using hash() for getting your integers.  This way it
can 
be a string of any length.  This is kinda neat I will probably always

use this method from here on out.  However I do not know if wxwidgets

can accept negative id's which has does return but that's not the end
of 
the world.  It can be worked around.
 >>> hash('a')
-468864544
 >>> help(hash)
Help on built-in function hash:

hash(...)
    hash(object) -> integer
   
    Return a hash value for the object.  Two objects with the same
value 
have
    the same hash value.  The reverse is not necessarily true, but
likely.

 >>> hash('b')
-340864157
 >>> b='b'
 >>> hash(b)
-340864157




		
_______________________________
Do you Yahoo!?
Express yourself with Y! Messenger! Free. Download now. 
http://messenger.yahoo.com


More information about the Tutor mailing list