-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug-caesar-4.py
56 lines (49 loc) · 1.87 KB
/
debug-caesar-4.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
# Module Lab: Caesar Cipher Program Bug #4
#
# In a previous lab, you created a Caesar cipher program. This version of
# the program is buggy. Use a debugger to find the bug and fix it.
# Double the given alphabet
def getDoubleAlphabet(alphabet):
doubleAlphabet = alphabet + alphabet
return doubleAlphabet
# Get a message to encrypt
def getMessage():
stringToEncrypt = input("Please enter a message to encrypt: ")
return stringToEncrypt
# Get a cipher key
def getCipherKey():
shiftAmount = input("Please enter a key (whole number from 1-25): ")
return shiftAmount
# Encrypt message
def encryptMessage(message, cipherKey, alphabet):
encryptedMessage = ""
uppercaseMessage = ""
uppercaseMessage = message.upper()
for currentCharacter in uppercaseMessage:
position = alphabet.find(currentCharacter)
newPosition = position + int(cipherKey)
if currentCharacter in alphabet:
encryptedMessage = encryptedMessage + alphabet[newPosition]
else:
encryptedMessage = encryptedMessage + currentCharacter
return encryptedMessage
# Decrypt message
def decryptMessage(message, cipherKey, alphabet):
decryptKey = -1 * int(cipherKey)
return encryptMessage(message, decryptKey, alphabet)
# Main program logic
def runCaesarCipherProgram():
myAlphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
print(f'Alphabet: {myAlphabet}')
myAlphabet2 = getDoubleAlphabet(myAlphabet)
print(f'Alphabet2: {myAlphabet2}')
myMessage = getMessage()
print(myMessage)
myCipherKey = getCipherKey()
print(myCipherKey)
myEncryptedMessage = encryptMessage(myMessage, myCipherKey, myAlphabet2)
print(f'Encrypted Message: {myEncryptedMessage}')
myDecryptedMessage = decryptMessage(myEncryptedMessage, myCipherKey, myAlphabet2)
print(f'Decrypted Message: {myDecryptedMessage}')
# Main logic
runCaesarCipherProgram()