Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selecting text as an input
#1
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.
Reply
#2
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.
"Often stumped... But never defeated."
Reply
#3
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  Modify Input() text catosp 6 2,825 Jun-08-2020, 10:48 PM
Last Post: deanhystad
  Python QGIS tool that replaces layout text labels with attributes from an input table geodenn92 1 2,628 Aug-13-2019, 06:05 AM
Last Post: buran
  Four text files input combinations into an output file mhyga 2 2,185 Jul-28-2019, 06:52 PM
Last Post: ThomasL
  removing tabs from input text jenya56 3 3,146 Mar-28-2019, 03:10 PM
Last Post: DeaD_EyE
  How do I shorten my input options? - text adventure ShiningKnight 3 4,615 Jan-01-2017, 03:21 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020