1

指示:

编辑:我只需要方法菜单返回“即将推出”,因为 - 就目前而言,如果用户输入 c、p 或 s,它什么也不返回。我看不出有什么合乎逻辑的原因。

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()
4

2 回答 2

1

在我提出我的解决方案之前,这里有一些评论。

  1. MethodMenu() 函数当前不返回任何内容。我认为您的意思是返回用户的选择。
  2. 我在 StartMenu() 和 MethodMenu() 之间看到了一种模式:每个都显示一个选择列表并重复获取用户的输入,直到用户输入正确的输入。但是,StartMenu() 函数不管理用户的输入,而 MethodMenu() 则在设计上 ==> 不一致。
  3. 由于获取用户输入并对其进行验证的行为发生了两次,因此最好将该代码块移动到可以调用的单独函数中,而不是多次编写相同的代码块。
  4. 我注意到单字母变量a的用户。一般来说,我建议使用更具描述性的名称,例如user_choiceuser_answeruser_input

事不宜迟,我的解决方案:

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()

更新

如果您必须知道 有什么问题MethodMenu(),这里是解释:用户第一次输入了正确的选择(c、p 或 s):整个 while 循环被跳过,这意味着“即将推出”将不会被打印。您可以修改您的解决方案,或者使用hek2mgl 的.

于 2013-03-23T05:44:41.150 回答
0

按照您的逻辑,您应该将函数更改MethodMenu()为:

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'    

但是为什么要使用a代替user_input或其他东西呢?!你应该使用富有表现力的变量名!;)

于 2013-03-23T05:45:13.667 回答