Search code examples
pythonfunctionfor-loopsignature

Is it possible to add this kind of default value to Caesar cypher python?


Here is a basic function call I would like to do, As you can see below in the signature, I would like to have the default value in s be the length of the string passed in for convenience. Is it possible to do this in python? Or some version of this?

def encrypt(text, s=len(text)):
        result = ""
    
        # transverse the plain text
        for i in range(len(text)):
            char = text[i]
            # Encrypt uppercase characters in plain text
    
            if (char.isupper()):
                result += chr((ord(char) + s - 65) % 26 + 65)
            # Encrypt lowercase characters in plain text
            else:
                result += chr((ord(char) + s - 97) % 26 + 97)
        return result

Solution

  • Here's the right way:

    def encrypt(text, s=None):
        if s is None:
            s = len(text)