说明:
编辑:我只需要方法菜单就可以返回“即将推出” - 因为它目前处于当前状态,如果用户输入,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()
答案 0 :(得分:1)
在我提出解决方案之前,以下是一些评论。
不用多说,我的解决方案:
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 。
答案 1 :(得分: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
或其他什么?!你应该使用富有表现力的变量名! ;)