CJCoding With Joseph
15free / daySign inCreate account

Caesar Cipher

Write def caesar(text: str, shift: int) -> str that returns the text with every letter moved shift places along the alphabet, wrapping from z back round to a; upper-case letters stay upper case, lower-case stay lower case, and any other character (spaces, commas, digits) is copied unchanged. Use ord(ch) to get a character's code number and chr(code) to turn a code back into a character: work out the letter's position from 0 to 25 by subtracting the code of a (or A), add the shift, use the remainder operator % with 26 to wrap, then add the code of a (or A) back; this also handles a shift larger than 25 and a negative shift. Build the result one character at a time with for ch in text. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, the text then the shift as an integer, which the tests read and pass to your function before printing the text it returns.

Expected Output:

Khoor, Zruog

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
FunctionsPractice Problems
📥 Auto-Input:
Hello, World\n3\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.