[Tutor] reiterative programming

Alan Gauld alan.gauld at blueyonder.co.uk
Mon Oct 27 13:51:49 EST 2003


I'm not sure I understand the logic of the conversion.

hex(ord(c))

takes the character c, converts it to a number equal
to its ASCII value and then turns that number into a
hex representation. (Try it in the interpreter:

>>> print ord('4')
52
>>> print hex(ord(4))
0x34

Are you sure you don't want the hex representation of
the number the character represents

>>> print hex(int('4'))
0x4

Or even

>>> print int('4',16)
4

> I want to loop through doing this until the returned value is a hex
4.
>
> while returnedval != '\0x4':

This checks for the string '\0x4' which treats the \ as an escape
and yields:.

>>> print '\0x4'
 x4

You could use a raw string:

>>> print r'\0x4'
\0x4

> For some reason that I don't understand, when the returnedval
> is '0x4', it does the loop again

Because '0x4' is not the same as '\0x4'!

You need to be very clear about what you actually want to compare
with what, and then check the actual format you are using.
The >>> prompt is your friend.

Alan G.





More information about the Tutor mailing list