Learn String Manipulation by Building a Cipher - Step 38

Tell us what’s happening:

This is what I have done but it not correct

Your code so far


/* User Editable Region */

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

for char in text.lower():
    if char is empty space True otherwise False:
        print(space!)
    index = alphabet.find(char)
    new_index = index + shift
    encrypted_text += alphabet[new_index]
    print('char:', char, 'encrypted text:', encrypted_text)

/* User Editable Region */

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 38

You can try by simplifying your if statement like this: if char == ' ': .
space isn’t a reserved word in Python.

1 Like

You need to really understand the concept of if statements, I suggest you read more about it and see some examples: https://www.geeksforgeeks.org/python-if-else/

if condition:
   # Statements to execute if
   # condition is true

if i > 15:
    print("10 is less than 15")

if i == " ":
    print("i is an empty space")

condition is an expression which, when evaluated, will either be true or false. You don’t need to write true and false.

5 == 5 will evaluate to true
5 == 10 will be false

if 5==5:
    print("this will print")

if 5 == 10:
    print("This will not print")

I hope this helps!

1 Like

Hi,

Struggling to see why this doesn’t work, please could you advise?

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

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

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

1 Like

I struggled with the same issue but found the problem.
You shouldn’t use the paranthesis for if condition. below is the final answer

Mod Edit: SOLUTION REMOVED

1 Like

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

Thanks, @MLearn !!
That worked for me, though not sure why we want the paranthesis removed.