4 回答

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)

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

TA貢獻1883條經驗 獲得超3個贊
問題是,當加或減 2 時,您不會檢查是否達到了字母表的限制(“a”或“z”)。你應該做類似的事情
if encoded_letter > "z": # do something

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)
應該管用
添加回答
舉報