repl.it
@malvoliothegood/

Encryption -using-a-dictionary

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# prints Welcome
print()
print('Welcome to the encryptor')
print("="*24)
# letters and their corresponding counterparts
# stored in a dictionary# Ref: https://www.diveinto.org/python3/native-datatypes.html
encoding = {
  "a": 'K',
  "b": "D",
  "c": "G",
  "d": "F",
  "e": "N",
  "f": "S",
  "g": "L",
  "h": "V",
  "i": "B",
  "j": "W",
  "k": "A",
  "l": "H",
  "m": "E",
  "n": "X",
  "o": "J",
  "p": "M",
  "q": "Q",
  "r": "C",
  "s": "P",
  "t": "Z",
  "u": "R",
  "v": "T",
  "w": "Y",
  "x": "I",
  "y": "U",
  "z": "O",
  " ": "!",
  "0": "9",
  "1": "8",
  "2": "7",
  "3": "6",
  "4": "5",
  "5": "4",
  "6": "3",
  "7": "2",
  "8": "1",
  "9": "0",
  ".": "@",
  "@": ".",
  "?": "#",
  "#": "?",
  "$": '"',
  '"': "$",
  ":": ",",
  ",": ":"
  }

# defines encode
def encode(note):  
  encoded_note = ""
  for char in note:
    encoded_note += encoding[char];
  return encoded_note

# # defines decode
def decode(encoded_note):  
  decoded_note = ""
  for char in encoded_note:
    for key, value in encoding.items():     
      if value.lower() == char:
        decoded_note += key
        continue    
  return decoded_note


# menu
EXIT = 0
ENCODE = 1
DECODE = 2
# Initial choice to start loop
choice = ENCODE

while (choice == 1 or choice == 2):
    choice = input('Would you like to encode or decode a note? (Enter 1 to encode, 2 to decode and 0 to exit): ')
    # choice is a string so convert it to an integer
    # non-numeric input will throw an exception when
    # choice is cast to an integer
    try:
      choice = int(choice)
    except ValueError:
      print('Non-numeric input! Try again')
      # Ensure program exits
      choice = 3

    if choice == ENCODE:
      note = input('What note would you like to encode? ')  
      encoded_note = encode(note.lower()) 
      print("Encoded note:", encoded_note)  
    elif choice == DECODE:
      note = input('What note would you like to decode? ') 
      decoded_note = decode(note.lower())
      print("Decoded note:", decoded_note)   
    elif choice == EXIT:        
      print('Exiting...')
    else: 
      print('Icorrect choice, exiting...')
?