Depending on your specific course version, you may need to enter this mapping into a configuration tool or write a short script to demonstrate it.
result = "" for i in range(0, len(code), 2): token = code[i:i+2] n = int(token) if n == 27: result += " " else if n == 28: result += "." else: result += chr(n - 1 + ord('A')) return result
Write a program that takes a string input from a user and converts it into a "coded" version based on your own unique rules. It should also be able to decode it back to the original message. Core Concepts Covered: Strings and Manipulation: Iterating through characters. Characters and ASCII: Using ord() and chr() in Python. 8.3 8 create your own encoding codehs answers
DECODING = code: char for char, code in ENCODING.items()
# 1. Get input from the user secret_message = input("Enter a message to encode: ") encoded_message = "" # 2. Loop through every character for char in secret_message: # 3. Apply substitution logic if char.lower() == 'a': encoded_message += "4" elif char.lower() == 'e': encoded_message += "3" elif char.lower() == 'i': encoded_message += "1" elif char.lower() == 'o': encoded_message += "0" elif char.lower() == 'u': encoded_message += "7" else: # Keep non-vowels the same encoded_message += char # 4. Print the final encoded string print("Encoded message: " + encoded_message) Use code with caution. Debugging & Passing CodeHS Autograders Depending on your specific course version, you may
| Mistake | Why It Happens | Fix | |---------|----------------|-----| | Forgetting to handle spaces | Space ( ' ' ) has ASCII 32. After shift, it becomes 37, which is '%' . Your decode must reverse correctly. | Test with "a b" to ensure spaces survive round-trip. | | Using a non-reversible rule | Example: multiplying by 2. Two different chars (like 'a'=97 and 'b'=98) could map to same number after mod. | Always use a bijective (one-to-one) rule. Addition/subtraction works perfectly. | | Returning a string instead of list | The prompt explicitly asks for a . | Use encoded_list.append(...) and return the list. |
: Display the final encoded string clearly to the user. Core Concepts: String Manipulation and ASCII Get input from the user secret_message = input("Enter
if == " main ": main()
You must be logged in to post a comment.