I am making a binary randomizing program that takes text in randomizes it and then output the randomized binary. But in the part that returns 7 digit binary objects to a list isn't working properly.
import random
message = input('What is your message? ')
def encrypter(message):
shuffled = []
second = []
ascii,binary = [],[]
for letter in message:
ascii.append(ord(letter))
for letter in ascii:
binary.append(int(bin(letter)[2:]))
print(binary)
for character in binary:
for digit in str(character):
shuffled.append(digit)
print(shuffled)
random.shuffle(shuffled)
print(shuffled)
previous_number = 0
current_string = ''
for digit in shuffled:
if len(current_string) < 7:
current_string += digit
previous_number = previous_number + 1
elif len(current_string) == 7:
second.append(int(current_string))
previous_number = 0
current_string = ''
I tried changing the elif statement into an else statement but still no luck. It seems to not be able to read the length of the strings. I also tried to change it from checking the length of the string to using a counter like
number = previous_number+1
if number < 7:
etc
To the best of my understanding, you want a chunked output of 7 digit numbers from the randomised binary representation of the input. This will do that. The length check was not failing, but you are resetting the output every time with current_string = ''
so, even if you fixed it to return an output, you just keep throwing away the previous string.
Basically, I have just taken the chunking code from the canonical, and then I have added a list comprehension to join the digits back up from the chunked list.
import random
message = input('What is your message? ')
def encrypter(message, chunk_size=7):
shuffled = []
second = []
ascii,binary = [],[]
for letter in message:
ascii.append(ord(letter))
for letter in ascii:
binary.append(int(bin(letter)[2:]))
print(binary)
for character in binary:
for digit in str(character):
shuffled.append(digit)
print(shuffled)
random.shuffle(shuffled)
print(shuffled)
chunked = [
shuffled[i:i + chunk_size] for i in range(0, len(shuffled), chunk_size)
]
joined = [''.join(item) for item in chunked]
return joined
result = encrypter(message=message)
print(result)
If my interpretation is correct (and, well you're using random.shuffle()
anyway) it should be noted that this is not "encryption" at all.