Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple encoding code
#1
Hi,

This simple encoding code converts text to ASCII and adds a one digit number to it. It seems to work ok, but while testing it, I noticed that if a 'v' is used, it's converted to ' ' when a 9 is added to it. (probably bc it's hitting ascii 127 - delete). Is there a way to fix it?

TIA

encoded_txt = ''
key = 0


def encode_text():
    global encoded_txt, key
    arry = []

    txt = input("Enter an alphanumeric text: ")
    while True:
        try:
            key = int(input('Enter a single digit number: '))
            if key > 9:
                n = 1 / 0  # force to fail
            break
        except:
            print("That's not a valid option!")

    # fill array with entered text
    for _ in range(len(txt)):
        arry.append(txt[_])

    # add pass to characters
    for char in range(len(arry)):
        x = ord(arry[char]) + key
        # convert ascii to char
        encoded_txt += chr(x)

    print("Encoded text:", encoded_txt)


encode_text()
Output:
Enter an alphanumeric text: vince Enter a single digit number: 9 New text: rwln Decoded text: vince
Reply
#2
Limit your alphabet to only visible characters. This example limits the alphabet to upper and lowercase letters,
import string
 
letters = string.ascii_uppercase + string.ascii_lowercase

def encode_text(text, shift):
    code = dict(zip(letters, letters[shift:] + letters[:shift]))
    return "".join([code[letter] for letter in text])

encoded = encode_text("vince", 9)
print(encoded, encode_text(encoded, -9))
ebolisa likes this post
Reply
#3
(Jun-18-2022, 12:45 AM)deanhystad Wrote: Limit your alphabet to only visible characters. This example limits the alphabet to upper and lowercase letters,

Thank you. It works even with a little modification:

letters = string.ascii_letters + string.digits + string.punctuation
Reply
#4
Maybe use ascii_printable. If you want punctuation you probably also want whitespace.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 227 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  help me simple code result min and max number abrahimusmaximus 2 869 Nov-12-2022, 07:52 AM
Last Post: buran
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,751 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,238 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,576 Sep-06-2021, 12:10 PM
Last Post: jefsummers
Sad SyntaxError: from simple python example file from mind-monitor code (muse 2) warmcupoftea 4 2,750 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Plotting sum of data files using simple code Laplace12 3 2,992 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  Help with isinstance command (very simple code) Laplace12 2 1,965 Jul-30-2020, 05:26 AM
Last Post: Laplace12
  Simple code help bntayfur 2 1,732 Jul-05-2020, 07:47 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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