Step 43 - Python Creating a Cipher

I do not know how to solve this because the instruction is too short

text = 'Hello World'
shift = 3
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted_text = ''

for char in text.lower():
    if char == ' ':
        encrypted_text += char
    index = alphabet.find(char)
    new_index = index + shift
    encrypted_text += alphabet[new_index]
    print('char:', char, 'encrypted text:', encrypted_text)

Can you link the challenge?

1 Like

Welcome to the forum @1302200651

To fix it, add an else clause after encrypted_text += char and indent all the subsequent lines of code except the print() call.

  1. You should create an else clause. Remember to include the final colon.
  1. For this challenge, after the if block - which means below encrypted_text += char add an else statement.

  2. Indent the lines below by four spaces, except the print call.

Happy coding

1 Like

The issue with your code is that it doesn’t handle wrapping around the alphabet when the index exceeds 25. Also, when the character is not found in the alphabet (like a space or punctuation), the program still tries to find and shift it, which causes errors.

Here’s a corrected version of your code:

solution removed by moderator

This ensures that it handles spaces and wraps the alphabet correctly.

1 Like

thank you for your interest in helping, this is a step in a project the project based curriculum, your concerns are taken care of going forward with the steps in the project.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like