Learn String Manipulation by Building a Cipher - Step 38

Tell us what’s happening:

Hello,

I’m working on a coding exercise and encountered an issue. The prompt asks me to print ‘space!’ in my if statement when a space is encountered, but it’s not accepting my solution.

Also, the encrypted_text in the if statement on line 8 is marked as incorrect, regardless of whether it contains a space. That why I believe the wrong not in there.

Could anyone provide some guidance? :woozy_face: Thank you!

Your code so far


/* User Editable Region */

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

for char in text.lower():
    if char ==' ':
        encrypted_text += ' '
        print('space!')
    else:
        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:

用戶代理是: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0

Challenge Information:

Learn String Manipulation by Building a Cipher - Step 38

Hello Pkj,

It seems like you are too fast for the excercise haha. You don’t have to add the space char to the encrypted text yet. You just have to print space.

thx a lot but it still not wrok

I had try to delete the code which is

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

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

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

but it still not pass the step

You’re still adding a space caharacter to encrypted text after the if expression:
encrypted_text += ' '
The question mention you doing this yet. If you remove this you should be able to pass it.

I did it! I’ve been stuck on this question for a while. Your help was greatly appreciated!

1 Like

I think it might be punctuation and space error.

Just try initialise your encrypted_text = ’ ’ to have empty space meaning quotaion marks with a single space in-between them and run it. I think it may resolve the issue.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.