Python Forum
how to decode this in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to decode this in python
#1
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))
Reply
#2
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 ?
Reply
#3
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.
Reply
#4
That's cool i shall move it to the homework part of the forum.
Reply
#5
random.seed (0) will also need to go at the start of the function def decode_text (text): for it to work properly
Reply
#6
The key is random.seed. If you set the same random seed, you will get the same sequence of random numbers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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 ?
Reply
#8
Not really, like I said I’m pretty new to programming do I don’t really understand all of this well.
Reply
#9
look up these in the documents to see what they are doing
https://docs.python.org/3/library/random...om.randint
https://docs.python.org/3/library/functi...ht=chr#chr
https://docs.python.org/3/library/functi...ht=ord#ord
Reply
#10
Do you think you could post the code and explain it that way? That’s probably help me understand it better
Reply


Forum Jump:

User Panel Messages

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