Newbie - Stuck

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Oct 8 08:36:58 EDT 2006


r3bol at yahoo.com:

Python is strongly typed, and it converts types automatically less
often than Perl.
The purpose of such strong(er) typing is to allow to catch some kind of
bugs, and to make the syntax simpler, more readable, etc.

> message = "abc"
> password = "z12"
> scrambled = message ^ password

^ is defined among integers:
>>> 55 ^ 126
73

You need more explicit code like:

>>> msg = "abc"
>>> pwd = "z12"
>>> [ord(c1) ^ ord(c2) for c1,c2 in zip(msg, pwd)]
[27, 83, 81]
>>> [chr(ord(c1)^ord(c2)) for c1,c2 in zip(msg, pwd)]
['\x1b', 'S', 'Q']
>>> "".join( chr(ord(c1)^ord(c2)) for c1,c2 in zip(msg, pwd) )
'\x1bSQ'


> I also wondered why this errored as well...
> int(messege)
> Is it not meant to convert a string to a number?

You can use int to truncate a float to int, or a string containing an
int to int (it accepts an optional base too):

>>> int("55")
55
>>> int(5.23)
5
>>> int("5.23")
Traceback
  ...
ValueError: invalid literal for int() with base 10: '5.23'
>>> int("abc")
Traceback
  ...

Bye,
bearophile




More information about the Python-list mailing list