我怎么解决这个问题?它一直告诉我,我不能把{elif}函数放在{CodedLetter}之后,我明白为什么,但是我也不能把{if}函数放在后面,因为同样的事情发生了,我不知道还能做什么。有人知道我可以用什么来代替{while}或{elif}。
letters = "abcdefghijklmnopqrstuvwxyz"
Welcome = print("Hello there...")
print("~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~")
print("¦ 1. Encrypt message ¦")
print("¦ 2. Decrypt message ¦")
print("¦ 3. Exit ¦")
print("~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~")
while Welcome is '1':
def Cipher(alphabet,UpperAlphabet,message,shift,FinalMessage):
for x in message:
if x in alphabet:
CodedLetter = alphabet[(int(alphabet.index(x))+shift)%(len(alphabet))]
FinalMessage += CodedLetter
elif x in UpperAlphabet:
CodedLetter = UpperAlphabet[(int(UpperAlphabet.index(x))+shift)%(len(UpperAlphabet))]
FinalMessage += CodedLetter
elif x == " ":
CodedLetter = x
FinalMessage += CodedLetter
return(FinalMessage)
alphabet = "".join(chr(x) for x in range(65,91))
UpperAlphabet = "".join(chr(x) for x in range(97,123))
message = input("Write your message")
shift = int(input("Enter shift amount"))
Word = []
FinalMessage = " "
print(Cipher(alphabet,UpperAlphabet,message,shift,FinalMessage))
while Welcome is '2':
def Cipher(alphabet,UpperAlphabet,message,shift,FinalMessage):
for x in message:
if x in alphabet:
CodedLetter = alphabet[(int(alphabet.index(x))+shift)%(len(alphabet))]
FinalMessage += CodedLetter
elif x in UpperAlphabet:
CodedLetter = UpperAlphabet[(int(UpperAlphabet.index(x))+shift)%(len(UpperAlphabet))]
FinalMessage += CodedLetter
elif x == " ":
CodedLetter = x
FinalMessage += CodedLetter
return(FinalMessage)
alphabet = "".join(chr(x) for x in range(65,91))
UpperAlphabet = "".join(chr(x) for x in range(97,123))
message = input("Write your message")
shift = int(input("Enter shift amount"))
Word = []
FinalMessage = " "
print(Cipher(alphabet,UpperAlphabet,message,shift,FinalMessage))
while welcome is '3':
exit()
你有很多重复的代码。。。我们来解决这个问题。。在
首先只为Caesar密码定义2个函数。一个用于加密和解密字符,另一个用于整个字符串。在
正数
shift
是加密,负数shift
是解密。在接下来,提示用户输入
^{pr2}$然后,对用户输入的选项进行一些if检查。在
把所有这些放在一个文件中,用和这里相同的缩进,你应该很好。在
运行示例如下
相关问题 更多 >
编程相关推荐