0

I am trying to decode enciphered message 'lmnopqrstuvwxyzabcdefghijk'. I got that it shifted by 11 and I must decipher 'I wtvp olel decfnefcpd lyo lwrzctesxd'.

Here's what I wrote so far:

#enciphered message = 'I wtvp olel decfnefcpd lyo lwrzctenter code hereesxd'

plain = 'abcdefghijklmnopqrstuvwxyz'
#plain Index= 0123456789........25 

cipher = 'lmnopqrstuvwxxyzabcdefghijk'
#11 12 13 14 15 16 17 18 19 20 21 22 23 24 25...12345678910

cipher_text = input('Enter enciphered message: ')
clean_text ='I '
for i in cipher_text:
    if cipher_text.index(i) != " ":
        clean_text = clean_text + plain.index(cipher[(ord(i)-ord('a'))])
    else:
        clean_text = clean_text + " "

print(clean_text)

When I ran it:

Enter enciphered message: I wtvp olel decfnefcpd lyo lwrzctesxd
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-97-ac338a9d79fc> in <module>
     18 for i in cipher_text:
     19     if cipher_text.index(i) != " ":
---> 20         clean_text = clean_text + plain.index(cipher[(ord(i)-ord('a'))])
     21         #chr((cipher.index(cipher_text[i]))+ ord('a')) - 11)
     22     else:

TypeError: can only concatenate str (not "int") to str

I am very new to Python, and I have no idea how to solve it.

add commend: The enciphered message that I want to decode has capital "I" as well as space between words, so I want to know how to decode capital and lowercase at the same time

  • Does this answer your question? [Caesar Cipher issue](https://stackoverflow.com/questions/26369035/caesar-cipher-issue) – colidyre Apr 03 '20 at 02:41
  • @colidyre No, :(, I am very newbie in Python, so I do want to understand about it, I have no idea about def things. Honestly, I have no idea how to decode it, and it is not working. if you have any chance, and time, could you please look at the code I made, and give me some comment to make it work? :( Thanks. – basicPythonhelp Apr 03 '20 at 02:59
  • @colidyre It also has capital "I" at the beginning of the sentence as well as space between words. :( – basicPythonhelp Apr 03 '20 at 03:04

1 Answers1

1

list.index() returns the index of that item's first occurence in the list. So the expression plain.index(cipher[(ord(i)-ord('a'))]) is an integer, which Python won't allow adding to a string.

What you probably want for the clean_text is the element in plain which has that index. So can use the standard list[index] notation to get the element: plain[plain.index(...)]

That line would now be:

clean_text = clean_text + plain[plain.index(cipher[(ord(i)-ord('a'))])]
>>> print(clean_text)
I wxxyzabcdefghhijlmnopqrstuv

Other changes/improvements:

  1. For the line if cipher_text.index(i) != " ":, the expression will never be true since list.index() returns an integer, or a ValueError if not found, which will never match as equal to a space " ". You also already have the character you're checking in the loop variable i. So that could be:

    if i != " ":
    
  2. There's a possible typo in cipher and cipher_text, the letter x appears twice.

  3. Wrt your comment:

    It also has capital "I" at the beginning of the sentence as well as space between words. :(

    That's from the line clean_text ='I ' just before the loop. Change that to clean_text =''.

  4. Don't concatenate strings with x = x + 'something new'. Keep appending each element to a list and then use str.join():

    # before the loop:
    clean_text = []  # empty list
    # inside the loop:
    clean_text.append(plain[plain.index(cipher[(ord(i)-ord('a'))])])
    # later, after the loop, put:
    ''.join(clean_text)
    
aneroid
  • 12,983
  • 3
  • 36
  • 66
  • Hi! I really appreciate your kind answer!, I tried again, but it still said 'IndexError: string index out of range' What happened to it :(, I am so sorry making you spend valuable time for me. – basicPythonhelp Apr 03 '20 at 03:07
  • Oh! Now it is working, but not decode :( If you do not mind, can I ask you little bit more about it? – basicPythonhelp Apr 03 '20 at 03:13
  • What I want to say in my comment, I wrote clean_text = 'I ' because I have no idea how to decode lowercase and uppercase at the same time. if you do not mind, could you provide me little more comments to make mine working properly? It is been a 7 days since I start learning Python :(, I really appreciate your help – basicPythonhelp Apr 03 '20 at 03:15
  • That's because of `cipher` and `plain` not being the same length (because of the double 'x': `xx` in the `cipher`). So the index for 'y' will actually give you 'z' and for 'z' you get an error. (Join the Python chat room, I have a few minutes - https://chat.stackoverflow.com/rooms/6/python) – aneroid Apr 03 '20 at 03:19
  • Unfortunately, and sadly, I can't :( what it said ' You must have 20 reputation on Stack Overflow to talk here. See the faq.' – basicPythonhelp Apr 03 '20 at 03:22
  • Thanks for trying!!! You are the best! One last question if possible! – basicPythonhelp Apr 03 '20 at 03:28
  • After I removed double x, it gave me "I n hega zwpw opnqypqnao wjz whcknepdio" for the decode.. :( is there any other way to get the right decipher :(? – basicPythonhelp Apr 03 '20 at 03:29
  • And is there any way to decipher capital and lowercase at the same time? I am sorry, I think that I have too many questions for you :(. If it bothers you, I am sorry – basicPythonhelp Apr 03 '20 at 03:31
  • 1. To decipher upper or lower characters, use `j = i.lower()` and replace `ord(i)` with `ord(j)`. That way, you only look up the index based on its lower case version. The output will be lowercase only. 2. If you want to return the same as the input, give it a think on how to approach it. Feel free to ask a separate question on StackOverflow if it's for a different problem. Also, [give this a read](https://stackoverflow.com/help/how-to-ask), and welcome to SO. – aneroid Apr 03 '20 at 03:34