Coded correspondence: string out of index error WHY!?

Hello
So on task 1 i’m already confused
In summary - we have to decode the message, it’s an offset of 10 from the usual alphabet.
So I wrong the first bit of code to create the “offset” list, which all runs fine and is stored as a list in coded_alphabet.

My approach is
For every letter in the coded message, go through the coded_alphabet, find the matching character, then add the character from the normal alphabet at the same index to the decoded_message list.
I am getting an error
File “script.py”, line 30, in
if letter == code[i]:
IndexError: string index out of range

I thought that using the while loop would stop that? I dont know why the index would go above that?
Also, do you think this solution is even going to work?

alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z'] coded_alphabet = [] i = 0 for letter in alphabet: letter = alphabet[i] if i < 16: new = i+ 10 code_letter = alphabet[new] coded_alphabet.append(code_letter) i+=1 else: new = i+10-26 code_letter = alphabet[new] coded_alphabet.append(code_letter) i+=1 print(coded_alphabet) coded_message = "xuo jxuhu! jxyi yi qd unqcfbu ev q squiqh syfxuh. muhu oek qrbu je tusetu yj? y xefu ie! iudt cu q cuiiqwu rqsa myjx jxu iqcu evviuj!" i=0 decoded_message = [] for letter in coded_message: i= 0 while i <= 26: for code in coded_alphabet: if letter == code[i]: decoded_message.append(alphabet[i]) elif letter == ' ': decoded_message.append(' ') else: i+= 1 print(decoded_message)

What if line 28 is written as,

while i < 26:

?

still says it’s out of range :frowning:

is code a single letter, a list, or a longer string?

No, it won’t.
The for-loop for code in coded_alphabet is a different loop, so you’ve increased i so that i becomes 26 or more in the inner loop, and this condition is not checked by the outer while loop until after that inner for-loop finishes running.

@java9731188620 makes a good point: What is ‘code’, and should it be subscriptable, being only a single letter? Your loop may need a tiny bit of revision.

I’d remove the while loop, but keep everything inside of it,
and change code[i] to be code since code is a single letter that you’re trying to find an index for (within coded_message).

You could change alphabet[i] to be alphabet[i % 26] to ensure that the index being used there stays in the range 0 to 25, if you think that is needed.

The shift may be in the opposite direction from what you’d expect, so you may need to change the 10 to be 16 and 16 to be 10 in your code or something like that.

You might also put break in the inner loop after the appropriate letter is appended so that the inner loop does not continue to run.

code where switch 10 and -10 to encode
for letter in alphabet:
    letter = alphabet[i]
    if i >= -16:
      new = i - 10
      code_letter = alphabet[new]
      coded_alphabet.append(code_letter)
      i+=1
    else:
      new = i - 10 + 26
      code_letter = alphabet[new]
      coded_alphabet.append(code_letter)
      i+=1

print(coded_alphabet)
code for loop with break in it
decoded_message = []

for letter in coded_message:
    i = 0
    for code in coded_alphabet:
      if letter == code:
        decoded_message.append(alphabet[i % 26])
        break
      elif letter == ' ' or letter in ['.', ',', '?', '!']:
        decoded_message.append(letter)
        break
      else:
        i += 1

print(''.join(decoded_message))