Reputation: 1
In Python 3 I'm trying to wright a little program that will encode and decode a phrase or sentence for you. Here comes the problem: I'm still very new to python and need some help. I want the program to convert the phrase of letters to a stream of numbers, and multiply it with the key the user will input. Then to decode the phrase, the numbers will be divided by the key and converted back in to letters.
TL;DR I want to change letters listed in a variable (Example: Message = hello hi) to numbers (A = 1, B = 2, etc, (8,5,12,12,15_8,9)), then be able to change it back.
This is the whole thing so far, so you can gauge my skill level a bit:
EorD = input('Encoding or Decoding? E/D: ')
if EorD == "E":
Key = input('Enter a number as your key: ')
Message = input('Enter a message to encode: ')
Encrypt = 1
if EorD == "D":
Key = input('Enter your key: ')
Message = input('Enter a message to decode: ')
Decrypt = 1
Encrypt = 0
import string
if Encrypt == 1:
print('Now encoding...')
elif Decrypt == 1:
print('Now decoding...')
A little explanation on how I should do this would be nice. I'm doing this to learn, so please explain any solutions!
Upvotes: 0
Views: 136
Reputation: 5394
Leading on from what Adam suggested is some code on roughly how you could go about it. delta = ord('A') - 1
is used to offset the ascii value so that 'A'
will start at 1. Note that 'a'
will then have a value of 33. Look at the ascii table linked by Adam for reference.
def encode(text, key):
delta = ord('A') - 1
return ', '.join(str((ord(c) - delta) * key) for c in text)
def decode(ords, key):
delta = ord('A') - 1
return ''.join(chr(o // key + delta) for o in ords)
def main():
while True:
coding = input('Encoding or Decoding? E/D: ').lower()
if coding == "e":
key = int(input('Enter a number as your key: '))
text = input('Enter a message to encode: ')
print(encode(text, key))
elif coding == "d":
key = int(input('Enter your key: '))
text = map(int, input('Enter a message to decode: ').split(', '))
print(decode(text, key))
else:
break
print()
# Output
>>> main()
Encoding or Decoding? E/D: e
Enter a number as your key: 1
Enter a message to encode: Hello World!
8, 37, 44, 44, 47, -32, 23, 47, 50, 44, 36, -31
Encoding or Decoding? E/D: d
Enter your key: 1
Enter a message to decode: 8, 37, 44, 44, 47, -32, 23, 47, 50, 44, 36, -31
Hello World!
Encoding or Decoding? E/D:
>>>
Bonus:
If you want you can also have a phrase as your key.
def conv_key(inp):
if inp.isdigit():
return int(inp)
return sum(ord(c) for c in inp)
Then change the int
casts to conv_key
like so
key = conv_key(input('Enter a number or phrase as your key: '))
key = conv_key(input('Enter your key: '))
>>> main()
Encoding or Decoding? E/D: e
Enter a number or phrase as your key: python
Enter a message to encode: Hello World!
5392, 24938, 29656, 29656, 31678, -21568, 15502, 31678, 33700, 29656, 24264, -20894
Encoding or Decoding? E/D: d
Enter your key: python
Enter a message to decode: 5392, 24938, 29656, 29656, 31678, -21568, 15502, 31678, 33700, 29656, 24264, -20894
Hello World!
Upvotes: 1
Reputation: 131
You can use ASCII values of characters using ord(character_here)
which returns the ASCII code and chr(ascii_code)
to go back to the character. - This is the ASCII table for reference - http://www.jimprice.com/ascii-0-127.gif
http://love-python.blogspot.co.uk/2008/04/convert-text-to-ascii-and-ascii-to-text.html
Upvotes: 0