-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (63 loc) · 2.53 KB
/
Copy pathmain.py
File metadata and controls
75 lines (63 loc) · 2.53 KB
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
import classes
class UserInterface(object):
'''
Serves as the primary user-program interface
'''
def __init__(self):
'''
Initializes a UserInterface object.
attributes:
self.lastInput (string, determined by user-inputted message for encoding or decoding)
'''
self.lastInput = None
def start(self):
'''
Starts the program
Returns: a string 'e', 'd', or 'exit' used to control the flow of the program
'''
while True:
userInput = input(('Would you like to decode (type d), or encode (type e)? Or you can type exit to exit. '))
if userInput == 'e':
return 'encode'
elif userInput == 'd':
return 'decode'
elif userInput == 'exit':
return 'exit'
else:
print('Invalid input!')
def decodeMode(self):
'''
Asks user for text and decodes the user-inputted cipher text
Returns: the decoded message, along with the shift used (tuple of the form ( shift(int), plain text(string) ) )
'''
userInput = input('Enter the text that you want to decode: ')
self.lastInput = userInput
message = classes.CiphertextMessage(userInput)
return message.decrypt_message()
def encodeMode(self):
'''
Asks user for plain text and shift, and encodes the text using the shift
Returns: the encoded message (string)
'''
userInputString = input('Enter the text you would like to encode: ')
while True:
try:
userInputShift = int(input('Enter the shift you would like to use to encode this text: '))
break
except:
print('Enter an integer please!')
message = classes.PlaintextMessage(userInputString, userInputShift)
return message.get_message_text_encrypted()
if __name__ == '__main__':
print('Welcome to the super secret caesar cipher machine!')
programInstance = UserInterface()
while True:
userInput = None
userInput = programInstance.start()
if userInput == 'encode':
print('I encoded it: %s' % (programInstance.encodeMode()))
elif userInput == 'decode':
print('I decoded it by shifiting by %d: %s' % programInstance.decodeMode())
else:
print('Thanks for using super secret caesar cipher!')
break