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


Messages In This Thread
Simple encoding code - by ebolisa - Jun-17-2022, 09:44 PM
RE: Simple encoding code - by deanhystad - Jun-18-2022, 12:45 AM
RE: Simple encoding code - by ebolisa - Jun-18-2022, 07:14 AM
RE: Simple encoding code - by deanhystad - Jun-18-2022, 10:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 360 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 514 Nov-07-2023, 04:32 PM
Last Post: snippsat
  help me simple code result min and max number abrahimusmaximus 2 951 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,838 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,405 Oct-04-2021, 05:18 PM
Last Post: snippsat
  My simple code don't works !! Nabi666 1 1,631 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,877 Jul-16-2021, 02:51 PM
Last Post: warmcupoftea
  Plotting sum of data files using simple code Laplace12 3 3,084 Jun-16-2021, 02:06 PM
Last Post: BashBedlam
  Help with isinstance command (very simple code) Laplace12 2 2,029 Jul-30-2020, 05:26 AM
Last Post: Laplace12
  Simple code help bntayfur 2 1,802 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