亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試包裝凱撒密碼(Python)

嘗試包裝凱撒密碼(Python)

躍然一笑 2024-01-16 15:32:20
我嘗試使用“if”語句來跳過非字母順序的字符,但它始終忽略代碼并添加移位的字符。我試圖將用戶輸入移動 2,但編碼字不應該有任何特殊字符,因此移動將從 za 繞回。下面的代碼是在我嘗試“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)
查看完整描述

4 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

要檢查字符是否是字母,可以使用 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)


查看完整回答
反對 回復 2024-01-16
?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

我認為你走在正確的軌道上,你只是要處理更多的邊緣情況,例如移位量是否大于 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))

用法示例encode:


Would you like to encode or decode text? encode/decode: encode

Enter in a word to encode: Yoruke-Stack-Overflow

Aqtwmg-Uvcem-Qxgthnqy

用法示例decode:


Would you like to encode or decode text? encode/decode: decode

Enter in the encoded word: Aqtwmg-Uvcem-Qxgthnqy

Yoruke-Stack-Overflow



查看完整回答
反對 回復 2024-01-16
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

問題是,當加或減 2 時,您不會檢查是否達到了字母表的限制(“a”或“z”)。你應該做類似的事情

if encoded_letter > "z": 
  # do something


查看完整回答
反對 回復 2024-01-16
?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

您所遵循的方法并不是最好的方法。但是,假設您僅考慮小寫字符。

編碼

替換

encoded_letter = chr(letter_ord + 2)

encoded_ord = letter_ord + 2if encoded_ord > ord('z'):  # after encoding if exceeds from 'z'
    difference = encoded_ord - ord('z')  # finding how much exceeded from 'z'
    encoded_ord = ord('a') + difference + 1  # restart from 'a' again.encoded_letter = chr(encoded_ord)

解碼

替換

decoded_letter = chr(letter_ord-2)

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 backwarddecoded_letter = chr(decoded_ord)

應該管用


查看完整回答
反對 回復 2024-01-16
  • 4 回答
  • 0 關注
  • 269 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號