+ 1

Python - How can we preserve spaces in this code?

I am trying to create a basic text encryptor: https://code.sololearn.com/c9ujk0a1CzGH/#py I was able to shift small letters as desired. However, I'm having trouble decrypting spaces. For example: input: hello there my name is solus 5 positions right-shifted: mjqqt%ymjwj%rd%sfrj%nx%xtqzx --> if I left shift 5 (ie. -5 right shift) this, I get: hello:there:my:name:is:solus --> the % got converted to : instead of spaces On line 19, I've tried to implement ' if ord(letter) != 32 ' but this appears to give a syntax error. How can I fix this bug?

29th Oct 2020, 1:22 AM
Solus
Solus - avatar
1 Answer
+ 2
This line: shift = position % 26 is a big part of your problem When you try to shift back (-5) shift is actually equal to 21 which will shift % (37) to : (58). The return value for getShiftedLetter() won't have an issue with this for a-z in most cases because of the modulo used to loop the letters. However, if you remove this part it will break the function. So, you really need to rewrite the whole thing. You may be able to get away with just splitting the input strings into words and then encrypt/decrypt each word, then join the words back into the string. In this case you should only pass values to the function that it can handle (a-z).
29th Oct 2020, 4:17 AM
ChaoticDawg
ChaoticDawg - avatar