Back to Freecodecamp

Step 21

curriculum/challenges/english/blocks/workshop-caesar-cipher/6819cf38880d8cc9b9f6af7a.md

latest1.3 KB
Original Source

--description--

Python allows you to specify a default value for the parameters in a function, creating a function that can be called with fewer or no arguments. Here's how to create a function with a name parameter that has a default value:

py
def greet(name='Polly'):
    return 'Hello ' + name
    
print(greet()) # Hello Polly

Add a third parameter named encrypt to your function and give it a default value of True.

--hints--

You should add a third parameter named encrypt with a default value of True to your function. Do not modify the existing parameters.

js
({ test: () => assert(runPython(`_Node(_code).find_function("caesar").has_args("text, shift, encrypt=True")`)) })

--seed--

--seed-contents--

py
--fcc-editable-region--
def caesar(text, shift):
--fcc-editable-region--

    if not isinstance(shift, int):
        return 'Shift must be an integer value.'

    if shift < 1 or shift > 25:
        return 'Shift must be an integer between 1 and 25.'

    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    translation_table = str.maketrans(alphabet + alphabet.upper(), shifted_alphabet + shifted_alphabet.upper())
    return text.translate(translation_table)

encrypted_text = caesar('freeCodeCamp', 3)
print(encrypted_text)