[Tutor] Python challenge and decryption

Joaquim Santos jsantos.lazer at gmail.com
Fri Dec 23 17:58:50 CET 2011


Hi again list!

I've been trying to implement the feedback I got. So far, I removed the
print statements from the function, limited the range (just for the z at
the moment) and tried to get all printing in one or two lines... but
without success...
I don't know if I'm doing the join as it should... The new list I ended up
trying to populate it with 2 different ways, but without success when
joined...

This is the new (and a improved) code:

 import string

def decrypt(cypheredText, shiftedCypherNumber):

'''

This function will take two arguments. The first is the cyphered text, the
second

is the number of characters we need to shift the text so we can decrypt it.

This is a Caesar cypher.

'''

for letter in cypheredText:

 asciiValue = ord(letter)

  if asciiValue in range(97, 123):

asciiValue += shiftedCypherNumber

 if asciiValue > 122:

asciiValue -= 26

 newLetter = chr(asciiValue)

 text = list()

text.append(newLetter)

 joinedText = ' '.join(text)

 return joinedText

  text = 'g fmnc wms bgblr rpylqjyrc gr zw fylb'

a = decrypt(text, 2)

print a


For the new list (text), I initially tried to do it like this -> text =
list(newLetter) and only now resorted to the append... which way is more
correct? Or which is not correct at all?

And why is the return giving me just one letter? (the last one...)


Thanks for all the tips so far. The maketrans like suggested probably would
be faster but I didn't knew about it and now that I'm trying to grind this
on my own I kind of prefer it... I'm learning more then if I used a
function or something... The ASCII table was invaluable in making me
rethink my approach!


Merry Christmas to all in the list! In case someone doesn't believe in it,
have a very nice weekend and all the best!


Joaquim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111223/da42e4d5/attachment.html>


More information about the Tutor mailing list