Reputation: 21
I'm trying to practice using dictionaries and functions on python. I am trying to write a program that encrypts a simple phrase or sentence with an encrypted alphabet:
Original alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted alphabet: TDLOFAGJKRICVPWUXYBEZQSNMH.
The user is to enter the phrase or sentence to be encrypted so it could contain upper or lowercase alphabet, spaces, commas, and periods. The output, though, should only be uppercase letters with spaces and punctuation marks.
cipher ={"A":"T","a":"T","B":"D","b":"D","C":"L","c":"L","D":"O","d":"O","E":"F","e":"F","F":"A","f":"A","G":"G","g":"G","H":"J","h":"J","I":"K","i":"K","J":"R","j":"R","K":"I","k":"I","L":"C","l":"C","M":"V","m":"V","N":"P","n":"P","O":"W","o":"W","P":"U","p":"U","Q":"X","q":"X","R":"Y","r":"Y","S":"B","s":"B","T":"E","t":"E","U":"Z","u":"Z","V":"Q","v":"Q","W":"S","w":"S","X":"N","x":"N","Y":"M","y":"M","Z":"H","z":"H"}
def encode(str,cipher):
result=""
for c in str:
result = result + cipher[c]
return result
Upvotes: 0
Views: 10540
Reputation: 424
Sometimes it's better to ask forgiveness then permission. You could remove half your key: values and replace them with str.upper(), that way small letters become big letters. If you call dict().get(key) you get the value of the key or you get None. The or operator between the dict and character evaluates to the character if the cipher.get(character) returns None.
def get_chiper(plaintext):
cipher = {"A": "T", "B": "D", "C": "L", "D": "O", "E": "F",
"F": "A", "G": "G", "H": "J", "I": "K", "J": "R",
"K": "I", "L": "C", "M": "V", "N": "P", "O": "W",
"P": "U", "Q": "X", "R": "Y", "S": "B", "T": "E",
"U": "Z", "V": "Q", "W": "S", "X": "N", "Y": "M",
"Z": "H"}
return "".join(cipher.get(character.upper()) or character
for character in plaintext)
And the full encoding and decoding could be done with the same function by reversing the dict.
def encode(plaintext, cipher):
return "".join(cipher.get(character.upper()) or character
for character in plaintext)
def decode(secret, encoding_cipher):
decode_cipher = {value: key for key, value in encoding_cipher.items()}
return encode(secret, decode_cipher)
def main():
cipher = {"A": "T", "B": "D", "C": "L", "D": "O", "E": "F",
"F": "A", "G": "G", "H": "J", "I": "K", "J": "R",
"K": "I", "L": "C", "M": "V", "N": "P", "O": "W",
"P": "U", "Q": "X", "R": "Y", "S": "B", "T": "E",
"U": "Z", "V": "Q", "W": "S", "X": "N", "Y": "M",
"Z": "H"}
plaintext = "hello foo from bar"
secret = encode(plaintext, cipher)
plaintext = decode(secret, cipher)
print(secret, plaintext)
if __name__ == '__main__':
main()
Upvotes: 2
Reputation: 27869
You should exclude those cases with unknown symbols and that could be done by:
cipher ={"A":"T","a":"T","B":"D","b":"D","C":"L","c":"L","D":"O","d":"O","E":"F","e":"F","F":"A","f":"A","G":"G","g":"G","H":"J","h":"J","I":"K","i":"K","J":"R","j":"R","K":"I","k":"I","L":"C","l":"C","M":"V","m":"V","N":"P","n":"P","O":"W","o":"W","P":"U","p":"U","Q":"X","q":"X","R":"Y","r":"Y","S":"B","s":"B","T":"E","t":"E","U":"Z","u":"Z","V":"Q","v":"Q","W":"S","w":"S","X":"N","x":"N","Y":"M","y":"M","Z":"H","z":"H"}
def encode(words, cipher):
result = ''
for letter in words:
if letter in cipher:
result = result + cipher[letter]
else:
result = result + letter
return result
phrase = raw_input('Please enter your phrase: ')
print encode(phrase, cipher)
Upvotes: 0