Jodi Peterson
Jodi Peterson

Reputation: 69

Need Assistance with my Encryption Program

Instructions:

Edit: I just need the Method Menu to return "coming soon" since - as it stands currently, if the user inputs, c, p, or s it returns nothing. I don't see the logical reason why.

def PrintDescription():
    print 'This program encrypts and descrypts messages using multiple \
encryption methods.\nInput files must be in the same directory as this program.\
\nOutput files will be created in this same directory.'

def StartMenu():
    print 'Do you wish to encrypt or decrypt?'
    print '<e>ncrypt'
    print '<d>ecrypt'
    print '<q>uit'

def MethodMenu():
  print 'Which method would you like to use?'
  print '<c>aesarian fixed offset'
  print '<p>seudo-random offset'
  print '<s>ubstitution cipher'
  a = raw_input("")
  while a not in ('c', 'p', 's'):
    if a:
      print "Error: You must type c, p, or s"
      a = raw_input("")
    if a == 'c' or a=='p' or a=='s':
      print 'Coming Soon'         

def main():
    alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?! \t\n\r"
    PrintDescription()
    a = None
    while a not in ('e', 'd', 'q'):
        if a:
            print "Error: You must type e, d, or q"
        else:
            StartMenu()
        a = raw_input("")
        if a == 'e' or a=='d':
          MethodMenu()
        if a == 'q':
          break  

main()

Upvotes: 1

Views: 168

Answers (2)

Hai Vu
Hai Vu

Reputation: 40773

Here are a few comments, before I present my solution.

  1. The MethodMenu() function currently does not return anything. I think you meant to return the user's choice.
  2. I see a pattern between the StartMenu() and MethodMenu(): each displays a list of choice and repeatedly getting the user's input until the user enters the correct input. However, the StartMenu() function does not manage the user's input, while the MethodMenu() does ==> inconsistency in design.
  3. Since the act of getting the user's input and validate it happens twice, it is a good idea to move that block of code into a separate function which you can call instead of writing the same block of code more than once.
  4. I notice the user of single-letter variable a. I general, I advice to use more descriptive names such as user_choice, user_answer, or user_input.

Without further ado, my solution:

def PrintDescription():
    print 'This program encrypts and descrypts messages using multiple \
encryption methods.\nInput files must be in the same directory as this program.\
\nOutput files will be created in this same directory.'

def GetChoice(acceptable_answers):
    while True:
        user_choice = raw_input('')
        if user_choice in acceptable_answers:
            return user_choice
        else:
            print 'Please try:', ', '.join(acceptable_answers)

def StartMenu():
    print 'Do you wish to encrypt or decrypt?'
    print '<e>ncrypt'
    print '<d>ecrypt'
    print '<q>uit'
    user_choice = GetChoice('edq')
    return user_choice

def MethodMenu():
    print 'Which method would you like to use?'
    print '<c>aesarian fixed offset'
    print '<p>seudo-random offset'
    print '<s>ubstitution cipher'
    user_choice = GetChoice('cps')
    return user_choice

def main():
    alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?! \t\n\r"
    PrintDescription()

    while True:
        user_choice = StartMenu()
        if user_choice in ('e', 'd'):
            user_choice = MethodMenu()
            # Do something based on the user_choice
        if user_choice == 'q':
            break

main()

Update

If you must know what is wrong with MethodMenu(), here is the explanation: The user typed the correct choice (c, p, or s) the first time: the whole while loop is skipped, that means 'Coming Soon' will not be printed. You can either revise your solution, or go with hek2mgl's.

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 158190

Following you logic you should change the function MethodMenu() to:

def MethodMenu():
  print 'Which method would you like to use?'
  print '<c>aesarian fixed offset'
  print '<p>seudo-random offset'
  print '<s>ubstitution cipher'
  a = None
  while a not in ('c', 'p', 's'):
    if a:
      print "Error: You must type c, p, or s"
    a = raw_input("")
    if a == 'c' or a=='p' or a=='s':
      print 'Coming Soon'    

But why using a instead of user_input or something else?! you should use expressive variable names! ;)

Upvotes: 0

Related Questions