My question is how to deal with the piece of code where I use Caesar's cipher .
The Decrypt and Encrypt functions must deal with the limits of the alphabet (A - Z and a - z) . I tried to write two possible loops for both alphabets in one cyclic function called cycencrypt .
But the function takes 6 arguments , and I read somewhere that is less readable and understandable, having more than 3 arguments in one function, so my question is:
Should I reduce the number of arguments by separating the two functions and bring the code snippet longer (but perhaps more understandable)? Thanks for any answer, I approve of this.
EDIT: Docstrings around functions have been removed to make the main goal of my question visible.
def offsetctrl(offset):
while offset < 0:
offset += 26
return offset
def cycleencrypt(string, offset, index, listing, first, last):
offset = offsetctrl(offset)
if string >= ord(first) and string <= ord(last):
string += offset
while string > ord(last):
string = ord(first) + (string - ord(last) -1)
listing[index] = chr(string)
Loop for encrypting with arguments and negative offset control
def encrypt(retezec, offset):
listing = list(retezec)
for index in range(0, len(retezec)):
string = ord(retezec[index])
cycleencrypt(string, offset, index, listing, 'A', 'Z')
cycleencrypt(string, offset, index, listing, 'a', 'z')
print(''.join(listing))
the main part of the encryption takes a lot of arguments in two lines with print
def decrypt(retezec, offset):
return encrypt(retezec, -offset)
if __name__ == "__main__":
encrypt("hey fellow how is it going", 5)
decrypt("mjd kjqqtb mtb nx ny ltnsl", 5)