如何循环此加密程序

时间:2013-12-17 21:25:07

标签: python python-2.7

我有这个代码,但我不知道如何让它循环,所以在完成编码或解码后它会重新启动菜单。它现在运行良好,不知道如何循环它。

import string

key = "qetuoadgjlxvnw ryipsfhkzcbm"
abc = "abcdefghijklmnopqrstuvwxyz "
abc_key = string.maketrans(abc, key)
key_abc = string.maketrans(key, abc)

def encode():
    """Encodes input text"""
    text = raw_input ("Please enter text to be encoded: ")
    text_lower = string.lower(text)
    text_lower;
    print text_lower.translate(abc_key);

def decode():
    """decyphers code"""
    code = raw_input ("Please enter code to be decyphered: ")
    code_lower = string.lower(code)
    code_lower;
    print code_lower.translate(key_abc);


# Welcome message
print "Welcome to Jake's Cryptography program!"

# Print menu
print "SECRET DECODER MENU"
print "0) Quit"
print "1) Encode"
print "2) Decode"

option = raw_input ("What do you want to do?")

if option == "0":
    print "Thank you for during secret spy stuff with me!"
elif option == "1":
    encode()
elif option == "2":
    decode()
else:
    print "Sorry, that is not an option."

任何帮助表示赞赏!

3 个答案:

答案 0 :(得分:2)

将其包裹在while语句中。像这样:

# Welcome message
print "Welcome to Jake's Cryptography program!"

# Print menu
while True:
    print "SECRET DECODER MENU"
    print "0) Quit"
    print "1) Encode"
    print "2) Decode"

    option = raw_input ("What do you want to do?")

    if option == "0":
        print "Thank you for during secret spy stuff with me!"
        break
    elif option == "1":
        encode()
    elif option == "2":
        decode()
    else:
        print "Sorry, that is not an option."

请注意break声明!

以上将每次打印菜单。如果您只想打印提示,请在菜单后(但在while True:行之前)向下移动raw_input行,然后修复缩进。

答案 1 :(得分:0)

# Welcome message here

option = -1
while option != 0:
    # Print menu
    # Raw input for the next option
    # Processing of the options

答案 2 :(得分:0)

option = 13

while (int(option) > 0):

    option = raw_input ("What do you want to do?")

    if option == "0":
        print "Thank you for during secret spy stuff with me!"
        print "and good night"
    elif option == "1":
        encode()
    elif option == "2":
        decode()
    else:
        print "Sorry, that is not an option."