[Tutor] Still the Python Challenge - Conclusion

Robert Sjoblom robert.sjoblom at gmail.com
Fri Jan 6 00:00:48 CET 2012


On 5 January 2012 19:41, Joaquim Santos <jsantos.lazer at gmail.com> wrote:
> Hi List!
[...]
> This is my script (I know that I only verify a small range but it
> works for what I want for now...)
>
> 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.
>    '''
>
>    textTranslated = list()
>
>    for letter in cypheredText:
>
>        asciiValue = ord(letter)
>
>
>        if asciiValue in range(97, 123):
>            asciiValue += shiftedCypherNumber
>
>            if asciiValue > 122:
>                asciiValue -= 26
>
>        newLetter = chr(asciiValue)
>
>
>        textTranslated.append(newLetter)
>
>    joinedText = ''.join(textTranslated)
>
>    return joinedText
>
>
> text = 'g fmnc wms bgblr rpylqjyrc gr zw fylb'
>
> a = decrypt(text, 2)
>
> print a
>
> and this is the end result:

First of all, congratulations on solving it. Second, please don't
include the answer to any python challenges to this list as future
members or people searching for help with the same particular
challenge might not want to know the answer. Thirdly, I think you
should read PEP 8 - Style Guide for Python Code, but I'll quote the
absolutely most relevant part:
>Use blank lines in functions, sparingly, to indicate logical sections.
You don't need a blank line after every line of code; in fact, it
makes it harder to read. Where does one block end and another begin?

>PEP 257 describes good docstring conventions.  Note that most importantly, the """ that ends a multiline docstring should be on a line by itself, and preferably preceded by a blank line,

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.

   '''
   textTranslated = list()
   for letter in cypheredText:
       asciiValue = ord(letter)
       if asciiValue in range(97, 123):
           asciiValue += shiftedCypherNumber
           #and so on

PEP 8 is really worth reading through: http://www.python.org/dev/peps/pep-0008/
there's also a link to PEP 257 in it. If you follow most of the
suggestions in PEP 8 it will be much easier to read your code, and
because of that easier to help you.

-- 
best regards,
Robert S.


More information about the Tutor mailing list