[Tutor] Python challenge and decryption
Steven D'Aprano
steve at pearwood.info
Mon Dec 19 23:42:21 CET 2011
Joaquim Santos wrote:
> - How can I make this "Human readable"? ... Print the letters in just one
> (or more) lines and maybe replace the " for spaces (This one I suppose it
> could/should be done with whitespaces() or just making a loop to check and
> change those for ' '.)
Firstly, you should not directly print anything in the function. You should
always separate *functionality* from *presentation*. The function should do
the job of doing the Caesar shift, and should return a string. This allows the
caller to store the result in a variable for further work. Presentation should
be handled by the caller, often (but not always) by using print.
So you should accumulate each character into a list, then when done combine
all the characters together into a string using ''.join(the_list), and finally
return the string.
Secondly, Caesar decryption should wrap around -- that is, it should only
apply to letters A...Z and only produce letters A...Z, and likewise for
lowercase. Anything not a letter shouldn't be shifted.
So it isn't enough to just add the shift to the letter. E.g. 'z' shifted by 1
gives '{', but needs to be 'a'.
If you have trouble implementing these two concepts, don't hesitate to ask for
additional hints and suggestions.
--
Steven
More information about the Tutor
mailing list