Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图包装凯撒·塞弗(Python)_Python_Python 3.x_Encoding - Fatal编程技术网

试图包装凯撒·塞弗(Python)

试图包装凯撒·塞弗(Python),python,python-3.x,encoding,Python,Python 3.x,Encoding,我曾尝试使用“if”语句跳过非字母顺序的字符,但它一直忽略代码并添加移位字符。我试图将用户输入移位2,但是编码的单词不应该有任何特殊字符,因此移位将从z-a返回 下面的代码是我尝试“if”语句之前的代码 encoded_word = ("") decoded_word = ("") def Encoding(text): global encoded_word letter_list = list(text) length_lis

我曾尝试使用“if”语句跳过非字母顺序的字符,但它一直忽略代码并添加移位字符。我试图将用户输入移位2,但是编码的单词不应该有任何特殊字符,因此移位将从z-a返回

下面的代码是我尝试“if”语句之前的代码

encoded_word = ("")
decoded_word = ("")

def Encoding(text):
    global encoded_word
    letter_list = list(text)
    length_list = len(text) 
    for i in range (0,length_list):
        letter_ord = ord(letter_list[i])
        encoded_letter = chr(letter_ord+2)
        encoded_word = (encoded_word + encoded_letter)
    print ("The encoded word is now:",encoded_word)

def Decoding(text):
    global decoded_word
    letter_list = list(text)
    length_list = len(text) 
    for i in range (0,length_list):
        letter_ord = ord(letter_list[i])
        decoded_letter = chr(letter_ord-2)
        decoded_word = (decoded_word + decoded_letter)
    print ("The decoded word is:",decoded_word)

decode_encode = str(input("Would you like to encode or decode text? encode/decode: "))
if decode_encode == "encode":
    user_word = str(input("Enter in a word to encode: "))
    Encoding(user_word)
if decode_encode == "decode":
    user_word = str(input("Enter in the encoded word: "))
    Decoding(user_word)

要检查字符是否为字母,可以使用isalpha()。 例如,应仅打印d和f:

    list = ["3","2","-","1","4","5","-","3","d", "f"]
    character_list = []

    for i in list:
        if i.isalpha():
            character_list.append(i)
        
    print (character_list)

问题是,当加或减2时,你没有检查你是否达到了字母表('a'或'z')的极限。你应该这样做

if encoded_letter > "z":
   # do something

你所遵循的方法不是最好的。但是,假设您只考虑小写字符

编码 替换

解码 替换

解码单词=字母单词-2
如果解码_ord

应该可以工作

我认为您在正确的轨道上,您只需要处理两个以上的边缘情况,例如移位量大于26,或者移位需要换行,等等。此外,使用
+
的字符串连接效率很低,因为每次连接都需要复制现有字符串。因此,考虑将其附加到字符列表中,而只在结尾创建输出编码/解码字符串:

SHIFT_AMOUNT = 2

def encode(text):
    res = []
    actual_shift_amount = SHIFT_AMOUNT % 26
    for ch in text:
        new_letter = ch
        if ord('a') <= ord(ch) <= ord('z'):
            if (ord(ch) + actual_shift_amount) <= ord('z'):
                new_letter = chr(ord(ch) + actual_shift_amount)
            else:
                new_letter = chr(ord('a') + actual_shift_amount - (ord('z') - ord(ch) + 1))
        elif ord('A') <= ord(ch) <= ord('Z'):
            if (ord(ch) + actual_shift_amount) <= ord('Z'):
                new_letter = chr(ord(ch) + actual_shift_amount)
            else:
                new_letter = chr(ord('A') + actual_shift_amount - (ord('Z') - ord(ch) + 1))
        res.append(new_letter)
    return ''.join(res)

def decode(text):
    res = []
    actual_shift_amount = SHIFT_AMOUNT % 26
    for ch in text:
        new_letter = ch
        if ord('a') <= ord(ch) <= ord('z'):
            if (ord(ch) - actual_shift_amount) >= ord('a'):
                new_letter = chr(ord(ch) - actual_shift_amount)
            else:
                new_letter = chr(ord('z') - actual_shift_amount + (ord(ch) - ord('a') + 1))
        elif ord('A') <= ord(ch) <= ord('Z'):
            if (ord(ch) - actual_shift_amount) >= ord('A'):
                new_letter = chr(ord(ch) - actual_shift_amount)
            else:
                new_letter = chr(ord('Z') - actual_shift_amount + (ord(ch) - ord('A') + 1))
        res.append(new_letter)
    return ''.join(res)


decode_or_encode = input("Would you like to encode or decode text? encode/decode: ").lower()
if decode_or_encode == "encode":
    user_word = input("Enter in a word to encode: ")
    print(encode(user_word))
elif decode_or_encode == "decode":
    user_word = input("Enter in the encoded word: ")
    print(decode(user_word))
示例用法
解码

Would you like to encode or decode text? encode/decode: encode
Enter in a word to encode: Yoruke-Stack-Overflow
Aqtwmg-Uvcem-Qxgthnqy
Would you like to encode or decode text? encode/decode: decode
Enter in the encoded word: Aqtwmg-Uvcem-Qxgthnqy
Yoruke-Stack-Overflow

试试看

谢谢你的帮助:)
decoded_ord = letter_ord - 2        
if decoded_ord < ord('a'):     # after decoding if deceeds from 'a'
    difference = ord('a') - decoded_ord   # finding how much deceeded from 'a'
    decoded_ord = ord('z') - difference + 1   # restart from 'z' again but backward
decoded_letter = chr(decoded_ord)
SHIFT_AMOUNT = 2

def encode(text):
    res = []
    actual_shift_amount = SHIFT_AMOUNT % 26
    for ch in text:
        new_letter = ch
        if ord('a') <= ord(ch) <= ord('z'):
            if (ord(ch) + actual_shift_amount) <= ord('z'):
                new_letter = chr(ord(ch) + actual_shift_amount)
            else:
                new_letter = chr(ord('a') + actual_shift_amount - (ord('z') - ord(ch) + 1))
        elif ord('A') <= ord(ch) <= ord('Z'):
            if (ord(ch) + actual_shift_amount) <= ord('Z'):
                new_letter = chr(ord(ch) + actual_shift_amount)
            else:
                new_letter = chr(ord('A') + actual_shift_amount - (ord('Z') - ord(ch) + 1))
        res.append(new_letter)
    return ''.join(res)

def decode(text):
    res = []
    actual_shift_amount = SHIFT_AMOUNT % 26
    for ch in text:
        new_letter = ch
        if ord('a') <= ord(ch) <= ord('z'):
            if (ord(ch) - actual_shift_amount) >= ord('a'):
                new_letter = chr(ord(ch) - actual_shift_amount)
            else:
                new_letter = chr(ord('z') - actual_shift_amount + (ord(ch) - ord('a') + 1))
        elif ord('A') <= ord(ch) <= ord('Z'):
            if (ord(ch) - actual_shift_amount) >= ord('A'):
                new_letter = chr(ord(ch) - actual_shift_amount)
            else:
                new_letter = chr(ord('Z') - actual_shift_amount + (ord(ch) - ord('A') + 1))
        res.append(new_letter)
    return ''.join(res)


decode_or_encode = input("Would you like to encode or decode text? encode/decode: ").lower()
if decode_or_encode == "encode":
    user_word = input("Enter in a word to encode: ")
    print(encode(user_word))
elif decode_or_encode == "decode":
    user_word = input("Enter in the encoded word: ")
    print(decode(user_word))
Would you like to encode or decode text? encode/decode: encode
Enter in a word to encode: Yoruke-Stack-Overflow
Aqtwmg-Uvcem-Qxgthnqy
Would you like to encode or decode text? encode/decode: decode
Enter in the encoded word: Aqtwmg-Uvcem-Qxgthnqy
Yoruke-Stack-Overflow