我该如何解决这个问题?它一直告诉我,我不能把{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()
答案 0 :(得分:1)
你有很多重复的代码......让我们解决这个问题..
首先只为Caesar Cipher定义2个函数。一个用于加密和解密一个字符,另一个用于整个字符串。
正shift
是加密,负shift
是解密。
def cipher_character(character, shift):
# Don't convert anything other than english characters
if not character.isalpha():
return character
# declare some helping constants
alpha_length = 26
ascii_shift = ord('A') if character.isupper() else ord('a')
cipher_shift = shift % alpha_length
# shift down to 0..25 for a..z
shifted = ord(character) - ascii_shift
# rotate the letter and handle "wrap-around" for negatives and value >= 26
shifted = (shifted + cipher_shift + alpha_length) % alpha_length
# shift back up to english characters
return chr(shifted + ascii_shift)
# Rotate a string k-positions
def cipher_string(string, shift):
return ''.join(cipher_character(c, shift) for c in string)
接下来,提示您输入用户
print("Hello there...")
print("~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~")
print("¦ 1. Encrypt message ¦")
print("¦ 2. Decrypt message ¦")
print("¦ 3. Exit ¦")
print("~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~")
option = input()
然后,对用户输入的选项进行一些if检查。
if option == '1' or option == '2':
message = input("Write your message: ")
if option == '1':
shift = int(input("Enter shift amount: "))
else:
shift = -1*int(input("Enter un-shift amount: "))
print(cipher_string(message, shift))
elif option == '3':
import sys
sys.exit(0)
将所有这些放在一个文件中,并使用与此处相同的缩进,您应该很好。
示例运行看起来像
$ python3 CeasarCipher.py
Hello there...
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
¦ 1. Encrypt message ¦
¦ 2. Decrypt message ¦
¦ 3. Exit ¦
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
1
Write your message: This is a test
Enter shift amount: 4
Xlmw mw e xiwx
$ python3 CeasarCipher.py
Hello there...
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
¦ 1. Encrypt message ¦
¦ 2. Decrypt message ¦
¦ 3. Exit ¦
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
2
Write your message: Xlmw mw e xiwx
Enter un-shift amount: 4
This is a test