所以我必須編碼一條訊息,但它是一種不同的編碼,如果輸入是CAT輸出必須是DQ6它應該編碼改變輸入的每個字母到鍵盤上的左上鍵,例如:in: bear out: G3Q4。我試圖在這樣的字典中對此進行編碼:
d1 = {"q": 1,"Q": 1,"w": 2,"W": 2,"e": 3,"E": 3,"r": 4,"R": 4,"t": 5,"T": 5,"y": 6,"Y": 6,"u": 7,"U": 7,"i": 8,"I": 8,"o": 9,"O": 9,"p": 0,"P": 0}
d2 = {"a": 'Q',"A": 'Q',"s": 'W',"S": 'W',"d": 'E',"D": 'E',"f": 'R',"F": 'R',"g": 'T',"G": 'T',"h": 'Y',"H": 'Y',"j": 'U',"J": 'U',"k": 'I',"K": 'I',"l": 'O',"L": 'O',"?": 'P',"?": 'P'}
d3 = {"z": 'A',"Z": 'A',"x": 'S',"X": 'S',"c": 'D',"C": 'D',"v": 'F',"V": 'F',"b": 'G',"B": 'G',"n": 'H', "N": 'H',"m": 'J',"M": 'J',",": 'K',".": 'L',"-": '?'}
我試過這個函式來檢查每個鍵,但我得到的一切都是“無”作為值。
text = input("Text: ")
def cif(text):
cifrd = ""
for i in text:
if i in d1:
cifrd = d1[(d1.index(i))%(len(d1))]
elif i in d2:
cifrd = d2[(d2.index(i))%(len(d2))]
elif i in d3:
cifrd = d3[(d3.index(i))%(len(d3))]
else:
cifrd = i
print("New text: ",cif(cifrd))
感謝任何幫助。

uj5u.com熱心網友回復:
您的編碼:
d1 = {"q": 1,"Q": 1,"w": 2,"W": 2,"e": 3,"E": 3,"r": 4,"R": 4,"t": 5,"T": 5,"y": 6,"Y": 6,"u": 7,"U": 7,"i": 8,"I": 8,"o": 9,"O": 9,"p": 0,"P": 0}
d2 = {"a": 'Q',"A": 'Q',"s": 'W',"S": 'W',"d": 'E',"D": 'E',"f": 'R',"F": 'R',"g": 'T',"G": 'T',"h": 'Y',"H": 'Y',"j": 'U',"J": 'U',"k": 'I',"K": 'I',"l": 'O',"L": 'O',"?": 'P',"?": 'P'}
d3 = {"z": 'A',"Z": 'A',"x": 'S',"X": 'S',"c": 'D',"C": 'D',"v": 'F',"V": 'F',"b": 'G',"B": 'G',"n": 'H', "N": 'H',"m": 'J',"M": 'J',",": 'K',".": 'L',"-": '?'}
有幾個問題。看我的評論
text = input("Text: ")
def cif(text):
cifrd = ""
for letter in text:
# There is no need to manually write out each dictionary and do a check
# Put the dictionaries in a list, iterate over each one, and if the letter
# is in the dictionary, you will get the respective letter back
for encode in [d1, d2, d3]:
# check if my letter is in the dictionary
actual = encode.get(letter)
# If you check a dictionary and the key is not there, you will get `None`, this if statement ensures you only append actual number/characters
if actual:
cifrd = str(actual)
# When using a function, return something if you need it outside of the function
return cifrd
decoded = cif(text)
print("New text: {}".format(decoded))
uj5u.com熱心網友回復:
您的代碼存在許多問題:
cif()您需要在函式末尾回傳“編碼”文本- 您需要將
text變數傳遞給cif()函式,而cifrd不是函式之外未定義的變數 - 字典沒有
.index()方法,您可以通過鍵訪問字典項,例如,d1["q"]回傳1。
對于它的價值,沒有必要維護三個單獨的字典,也沒有理由在字典中同時保留小寫和大寫字母;存盤小寫或大寫鍵,并在訪問翻譯時將輸入轉換為正確的大小寫,即input "Q" -> lowercase "q" -> d1["q"].
這里:
mapping = {'q': 1, 'w': 2, 'e': 3, 'r': 4, 't': 5, 'y': 6, 'u': 7, 'i': 8, 'o': 9, 'p': 0,
'a': 'q', 's': 'w', 'd': 'e', 'f': 'r', 'g': 't', 'h': 'y', 'j': 'u', 'k': 'i', 'l': 'o', '?': 'p',
'z': 'a', 'x': 's', 'c': 'd', 'v': 'f', 'b': 'g', 'n': 'h', 'm': 'j', ',': 'k', '.': 'l', '-': '?'}
def cif(s: string) -> string:
encoded_string = ""
for char in s:
encoded_string = mapping.get(char.lower(), char) # leaves the character un-encoded, if the character does not have a mapping
return encoded string
我實際上建議使用str.translate(). 您可以傳遞兩個字串,第一個是輸入字符,第二個是這些輸入應映射到的字符:
t = str.maketrans("qwertyuiopasdfghjkl?zxcvbnm,.-", "1234567890qwertyuiopasdfghjkl?")
"hello world".translate(t)
'y3oo9 294oe'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/436881.html
