(Apr-07-2019, 08:50 PM)anthoniegallego Wrote: Do you think you could post the code and explain it that way? That’s probably help me understand it better
I can explain the code you have so far, maybe that will help you take it to the level you need it at.
# import the random module, allowing you to use its functions
import random
# this is the function which takes a string of text as parameter and encodes it
def encode_text (text):
# the seed generates a pattern based on the seed
# so if you want the same pattern later, use the same seed
random.seed (0)
# declaring an empty string
enc_text = ''
# a string in python is a sequence of text so we can iterate through it
for ch in text:
# I added this to demonstrate what the value of ch is in each iteration
print(ch)
# enc_number is going to be a random integer like this: 0 < N < 2
# more on this here: https://docs.python.org/3.7/library/random.html#random.randint
# note: this number is based on the seed from earlier
enc_number = random.randint (0, 2)
# let's see what enc_number is equal to to help you visualize
print(enc_number)
# enc_char is wrapped around in a chr() so this means that the value
# that it returns is converted to a char. let's examine this further:
# before it is converted this statement executes: (ord (ch) + enc_number)
# ord() basically converts a character (in this case, ch is our character)
# and then returns our integer. check this link out:
# https://docs.python.org/3.7/library/functions.html#ord
# finally, ord (ch) is added to the enc_number from earlier, resulting in an integer
# which is why the statement is wrapped in chr(), because it has to convert this integer to a character
enc_char = chr(ord (ch) + enc_number)
# demonstrate it to help visualize:
print(f"ch: {ch} \t ord(ch): {ord(ch)}")
# finally, we add the value of enc_char to our empty string,
# and we keep doing it through each iteration of our for loop
enc_text += enc_char
# our function returns the enc_text string that our for loop built
return enc_text
print(encode_text('test'))
Check it out, I commented your enc_text function and tried to help you better understand it. I added a few extra print statements too so that you can just see better exactly what's happening, and what variables are equal to what. Sorry if there are grammar errors in the comments, or they don't meet "pep" standards, I didn't really open the code in my IDE, just commented the code snippet assuming it is already working and OP just needs to do the decode function.
I'm at the computer
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):
random.seed(0)
dec_text = ''
for ch in text:
dec_number = random.randint(0, 2)
dec_char = chr(ord(ch) - dec_number)
dec_text += dec_char
return dec_text
>>> text = "test123daksjd32ud"
>>> print(f"Original string: {text}")
Original string: test123daksjd32ud
>>> encoded_text = encode_text(text)
>>> print(f"Encoded string: {encoded_text}")
Encoded string: ufsu334eblujf33ud
>>> decoded_text = decode_text(encoded_text)
>>> print(f"Decoded string: {decoded_text}")
Decoded string: test123daksjd32ud