Newbie - Stuck

R

r3bol

The perl version of this code works but not the python version. What am
I doing wrong?

message = "abc"
password = "z12"

scrambled = message ^ password

I also wondered why this errored as well...

int(messege)

Is it not meant to convert a string to a number?
 
D

Diez B. Roggisch

The perl version of this code works but not the python version. What am
I doing wrong?

message = "abc"
password = "z12"

scrambled = message ^ password

I also wondered why this errored as well...

int(messege)

Is it not meant to convert a string to a number?

Including error messages & stacktraces helps tremendously when asking
such questions - especially on a weekend, when our crystall balls are in
maintenance in the elves headquarters at the north pole.

Diez
 
B

bearophileHUGS

(e-mail address removed):

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: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):
Traceback
...
ValueError: invalid literal for int() with base 10: '5.23'Traceback
...

Bye,
bearophile
 
P

Paddy

The perl version of this code works but not the python version. What am
I doing wrong?

message = "abc"
password = "z12"

scrambled = message ^ password

I also wondered why this errored as well...

int(messege)

Is it not meant to convert a string to a number?
Hmm,
Looks like you need to learn more elementary Python before we can even
attempt to help you more, but here goes anyway...

Python has strict typing compared to Perl (a good thing)!
The string "abc" is not a valid representation of an integer in Python,
and, unlike Perl, Python will complain about its use when an integer
is expected:

$ perl -e 'print "abc" ^ 123, "\n"'
123

$ python -c 'print "abc" ^ 123'
Traceback (most recent call last):
File "<string>", line 1, in ?
TypeError: unsupported operand type(s) for ^: 'str' and 'int'

$

Please don't give up on Python. It *is* different to Perl.

- Paddy.
 
R

r3bol

Thanks everyone. I guess I was a little quick to think I could jump
straight into Python. I understand the principals of what was said but
not the code just yet. I was always under the impression that python
was a cleaned up version of perl. I guess this excercise put me
straight! I will try and get through a lot more beginners tutorials
before attempting a stunt like that again ;)
Python has surprised me already (a good thing!)
Regards.
 
C

Christoph Haas

The perl version of this code works but not the python version. What am
I doing wrong?

message = "abc"
password = "z12"

scrambled = message ^ password

"abc" XOR "z12"? Strings don't have an __xor__ method.
I also wondered why this errored as well...

int(messege)

Is it not meant to convert a string to a number?

If you were Python how would you convert "abc" to a number? :)

Yes, I know Perl makes "0" from anything that doesn't look like a number
but Python's principle is to never hide errors while Perl makes certain
assumptions. So Python prefers to complain.

Christoph
 
G

Gary Herron

The perl version of this code works but not the python version. What am
I doing wrong?

message = "abc"
password = "z12"

scrambled = message ^ password
The error message (which you should have supplied) tells you why it
fails. That operator does not operate on strings. In fact, it operates
on integers and longs.
I also wondered why this errored as well...

int(messege)

Is it not meant to convert a string to a number?
It does convert a string to a number, but just what number do you think
"abc" should be converted to ? 42 perhaps?

int("123') will yield 123,
and if you decide to treat "abc" as a hexadecimal number, then
int("abc", 16)" yields 2748.

If (as I believe) you wish to take the bit patterns used internally to
represent a string and reinterpret those bits as an integer, then ...
perhaps you'd like to check out the struct module.

Gary Herron
 
T

Theerasak Photha

Yes, I know Perl makes "0" from anything that doesn't look like a number
but Python's principle is to never hide errors while Perl makes certain
assumptions. So Python prefers to complain.

And raises real exceptions moreover, instead of leaving you with a
string and holding the bag. I still like Perl after a fashion, but
c'mon, Konrad Zuse thought of real exceptions handling in
19--bleeding--46.

-- Theerasak
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top