MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / main

Function main

src/gamesbyexample/simplesubcipher.py:18–68  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
17
18def main():
19 print('''Simple Substitution Cipher, by Al Sweigart
20A simple substitution cipher has a one-to-one translation for each
21symbol in the plaintext and each symbol in the ciphertext.''')
22
23 # Let the user specify if they are encrypting or decrypting:
24 while True: # Keep asking until the user enters e or d.
25 print('Do you want to (e)ncrypt or (d)ecrypt?')
26 response = input('> ').lower()
27 if response.startswith('e'):
28 myMode = 'encrypt'
29 break
30 elif response.startswith('d'):
31 myMode = 'decrypt'
32 break
33 print('Please enter the letter e or d.')
34
35 # Let the user specify the key to use:
36 while True: # Keep asking until the user enters a valid key.
37 print('Please specify the key to use.')
38 if myMode == 'encrypt':
39 print('Or enter RANDOM to have one generated for you.')
40 response = input('> ').upper()
41 if response == 'RANDOM':
42 myKey = generateRandomKey()
43 print('The key is {}. KEEP THIS SECRET!'.format(myKey))
44 break
45 else:
46 if checkKey(response):
47 myKey = response
48 break
49
50 # Let the user specify the message to encrypt/decrypt:
51 print('Enter the message to {}.'.format(myMode))
52 myMessage = input('> ')
53
54 # Perform the encryption/decryption:
55 if myMode == 'encrypt':
56 translated = encryptMessage(myMessage, myKey)
57 elif myMode == 'decrypt':
58 translated = decryptMessage(myMessage, myKey)
59
60 # Display the results:
61 print('The %sed message is:' % (myMode))
62 print(translated)
63
64 try:
65 pyperclip.copy(translated)
66 print('Full %sed text copied to clipboard.' % (myMode))
67 except:
68 pass # Do nothing if pyperclip wasn't installed.
69
70
71def checkKey(key):

Callers 1

simplesubcipher.pyFile · 0.70

Calls 4

generateRandomKeyFunction · 0.70
checkKeyFunction · 0.70
encryptMessageFunction · 0.70
decryptMessageFunction · 0.70

Tested by

no test coverage detected