Coded Correspondence - Turning List into String?

I’m sure this is a quick answer, but I’m having trouble turning my list into a string for the Coded Correspondence project. Here’s what I’ve gotten so far:

The output is working for the decrypted_message list, but line 17 isn’t turning it into a string and I’m not sure why. Line 21 just doesn’t print anything.

Can someone point me in the right direction?

Pasting your code (with proper formatting) is usually more helpful than a screenshot, as it allows forum users to run and experiment with the code themselves. To preserve code formatting in forum posts, see: How do I format code in my posts?

In your code, you have declared and initialized decrypted_message and message_string as an empty list and empty string, respectively. Then, you have defined the decode_cipher function. Finally, you have made a function call and executed a couple of print statements.

In your function, when you append letters to the decrypted_message list, it mutates the existing list and when you print this list after the function call, it shows up successfully.

But, the same is not true for message_string. It is not a list and is immutable. When you execute this statement (within the function),

message_string = "".join(decrypted_message)

you are actually assigning the joined string to a variable which is within the scope of the function (and can’t be seen outside the function). This message_string variable is different than the message_string variable you defined and initialized at the top of your code. Instead, you want to return this joined string AND then assign the returned string to a variable outside the function. Something like:

def decode_cipher(message, offset):
    # some code
    # more code
    message_string = "".join(decrypted_message)
    return message_string  # This allows you to send the 
                           # result outside your function

message_string = decode_cipher("xuo ... iuj!", 10) 
# The string returned by the decode_cipher function is assigned 
# to the message_string variable you declared at the top. 
# It overwrites the empty string with the string returned by the function.
print(decrypted_message)
print(message_string)
3 Likes

This is how I did mine!

alphabet_letters = [“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”]
ciphered_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”

#print(alphabet_letters.index(“x”))

deciphered_message =
for letter in ciphered_message:
letter == alphabet_letters
for item in alphabet_letters:
if item == letter:
deciphered_message.append(alphabet_letters[(alphabet_letters.index(item) + 10) % 26])

else:
if letter == " “:
deciphered_message.append(” “)
else:
if letter == “!”:
deciphered_message.append(”!“)
else:
if letter == “?”:
deciphered_message.append(”?")

deciphered_message_string = “”.join(str(element) for element in deciphered_message)
print(deciphered_message_string)

hope it wasnt a merry go round