
def encode(text): """ Encodes a string by shifting every letter by 1. Example: 'abc' becomes 'bcd' """ result = "" for char in text: # Check if the character is a letter if char.isalpha(): # Convert to ordinal, shift, and convert back # We use ord to get the ASCII number, add 1, and chr to get the letter back new_char = chr(ord(char) + 1) result += new_char else: # If it's not a letter (like punctuation or space), leave it as is result += char
Because strings in Python are immutable (unchangeable), you cannot modify the user's input string directly. Instead, you initialize an empty string ( encoded_result = "" ) and use the += operator to build a brand-new string piece by piece inside the for loop. Best Practices for Passing the CodeHS Autograder
(But you can choose any mapping—just be consistent.)