Python Forum
how to decode this in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: how to decode this in python (/thread-17339.html)

Pages: 1 2


how to decode this in python - anthoniegallego - Apr-07-2019

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))



RE: how to decode this in python - Yoriz - Apr-07-2019

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 ?


RE: how to decode this in python - anthoniegallego - Apr-07-2019

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.


RE: how to decode this in python - Yoriz - Apr-07-2019

That's cool i shall move it to the homework part of the forum.


RE: how to decode this in python - Yoriz - Apr-07-2019

random.seed (0) will also need to go at the start of the function def decode_text (text): for it to work properly


RE: how to decode this in python - ichabod801 - Apr-07-2019

The key is random.seed. If you set the same random seed, you will get the same sequence of random numbers.


RE: how to decode this in python - Yoriz - Apr-07-2019

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 ?


RE: how to decode this in python - anthoniegallego - Apr-07-2019

Not really, like I said I’m pretty new to programming do I don’t really understand all of this well.


RE: how to decode this in python - Yoriz - Apr-07-2019

look up these in the documents to see what they are doing
https://docs.python.org/3/library/random.html?highlight=random#random.randint
https://docs.python.org/3/library/functions.html?highlight=chr#chr
https://docs.python.org/3/library/functions.html?highlight=ord#ord


RE: how to decode this in python - anthoniegallego - Apr-07-2019

Do you think you could post the code and explain it that way? That’s probably help me understand it better