A Menu to Wrap it Up
FUNCTION – def show_menu(min, max, quit):
FUNCTION – def getInteger(min, max, message, checkRange = True):
FUNCTION – def getCharacter(min, max, message):
FUNCTION – def getValue(message = "Enter choice: "):
That is easy to say, but actually accounts for a lot of the Python code. The menu also allows other things to be set, like the wheel number and starting key etc.
First we have to print the menu, then get the User choice, and then perform the action. This section of code ties all that together. Notice that it is basically a big if statement, inside a while loop, that is dependant upon the User choice – restricting the User choice to numbers helps to simplify things.
Pertinent bits of code
while 1:
showMenu(menuMin, menuMax, menuQuit) #Show the menu.
#Get the user choice, without checking the range.
userChoice = getInteger(0, 0, "Enter choice [%d-
#Take action as per selected menu-
if userChoice == menuQuit:
break #Leave the while loop.
elif userChoice == 1:
selectedWheel = getInteger(1 ,2 ,"Enter Coding Wheel [1 or 2]: ")
elif userChoice == 2:
pointerChar = getCharacter('A', 'Z', "Enter Pointer Position [A to Z]: ")
pointerInt = ord(pointerChar)-
elif userChoice == 3:
codeStartChar = getCharacter('A', 'Z', "Enter Coding Start Position [A to Z]: ")
codeStartInt = ord(codeStartChar)-
elif userChoice == 4:
increment = getInteger(-
elif userChoice == 5:
blocksize = getInteger(1, 10, "Enter Block Size [1 to 10]: ")
elif userChoice == 6:
plaintext = getValue("Enter Plaintext: ")
print("Encryption: %s => %s" % (plaintext, encrypt(plaintext)))
elif userChoice == 7:
cipher = getValue("Enter Cipher: ")
print("Plaintext: %s => %s" % (cipher, decrypt(cipher)))
else:
print("Error: \"%d\" is not a valid choice!" % userChoice)