Python Forum
Selecting text as an input - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Selecting text as an input (/thread-27185.html)



Selecting text as an input - lazerwolf101 - May-28-2020

The purpose of my code is to decrypt text in the variable "message". When you run the code it will show all the keys from 1 to 25. And after it will ask an input from a user to choose which key they want. Up to this point the code has worked perfectly but when I input which key I want, it does not print it. That is what I need help with. Here is the code:
message = 'khoorCpACqdphClvColdp'
LETTERS = 'abcdefghijklmnopqrstuvwxyz'
 
for key in range(len(LETTERS)):    

     translated = ''
    
     for symbol in message:
         if symbol in LETTERS:

             num = LETTERS.find(symbol) 
             num = num - key

             if num < 0:

                 num = num + len(LETTERS)

             translated = translated + LETTERS[num]
         else:
             translated = translated + symbol
     print('Key #%s: %s' % (key, translated))

option = input("Select your decrypted text. ")
if option == key:
        print(option)
[Image: spq2wz]

I just need help with at the end please. that would be appreciated thanks.


RE: Selecting text as an input - DT2000 - May-29-2020

You seem to be trying to decrypt a Caesar cypher. I have posted on this here.
It would help if you showed the whole code so we can see if the message is being encrypted correctly as well.


RE: Selecting text as an input - lazerwolf101 - May-29-2020

(May-29-2020, 03:05 AM)DT2000 Wrote: You seem to be trying to decrypt a Caesar cypher. I have posted on this here.
It would help if you showed the whole code so we can see if the message is being encrypted correctly as well.

The encryption code isn't needed I made my own and works fine. I just need help with the very last lines of code in the decryption. Where the user is asked to input which key they want. and once the user has inputted what they want, that key will then be printed.

Here is my encryption code btw:
def encrypt():
        global encrypted_message
        e_message = input("[E]Enter your message: ")
        e_key = input("[E]Enter your shift: ")
        e_key = int(e_key)
        print(e_message)
        print(e_key)

        seperate_message = [a for a in e_message]
        print(seperate_message)

        ascii_message = [ord(a) for a in e_message]
        print(ascii_message)

        shifted_message = [ord(a)+e_key for a in e_message]
        print(shifted_message)

        encrypted_message = " "

        for a in e_message:
                if a in alphabet:
                        encrypted_message += alphabet[(alphabet.index(a)+e_key)%(len(alphabet))]


        print("Your encrypted message is: " + encrypted_message)
        pass


Decryption code:
LETTERS = 'abcdefghijklmnopqrstuvwxyz'



d_message = input("Enter your message: ")
print(d_message)
for key in range(len(LETTERS)):    

     translated = ''
    
     for symbol in d_message:
         if symbol in LETTERS:

             num = LETTERS.find(symbol) 
             num = num - key

             if num < 0:

                 num = num + len(LETTERS)

             translated = translated + LETTERS[num]
         else:
             translated = translated + symbol
     print('Key #%s: %s' % (key, translated))

option = input("Select your decrypted text. ")
print(option)