Python Forum

Full Version: how to decode this in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
hi, im fairly new to python and i was wondering how i could finish this code:

import random

def encode_text (text):
    random.seed (0)

    enc_text = ''

    for ch in text:
        enc_number = random.randint (0, 2)
        enc_char = chr(ord (ch) + enc_number)
        enc_text += enc_char
    return enc_text


def decode_text (text):
   
    dec_text = ''

# Add code to decode the message that was encoded wint enccode_text (text) function

    return dec_text
the code above would be imported into the following:

import enc_function


text = 'Uie!svjdl!drqwo fqy"lwmqs"oxfs"tif!ncz{!eqh'

print ("Original string: ", enc_function.decode_text (text))
By finish it do you mean the part
# Add code to decode the message that was encoded wint enccode_text (text) function
is your homework ?
Yea I do mean that part and it is my homework, but I don’t want anyone to just give me the answer, I want someone to help me understand it more because I don’t know how it works.
That's cool i shall move it to the homework part of the forum.
random.seed (0) will also need to go at the start of the function def decode_text (text): for it to work properly
The key is random.seed. If you set the same random seed, you will get the same sequence of random numbers.
you just need to reverse what is happening in the function def encode_text (text):
do you understand what encode_text is doing to be able to reverse it ?
Not really, like I said I’m pretty new to programming do I don’t really understand all of this well.
Do you think you could post the code and explain it that way? That’s probably help me understand it better
Pages: 1 2