gpt4 book ai didi

python - 如何循环这个密码程序

转载 作者:行者123 更新时间:2023-12-01 05:29:20 25 4
gpt4 key购买 nike

我有这段代码,但我不确定如何使其循环,以便在完成编码或解码后它会恢复菜单。现在运行良好,只是不知道如何循环它。

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."

感谢任何帮助!

最佳答案

将其包裹在 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 行之前),然后修复缩进。

关于python - 如何循环这个密码程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20645002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com