我正在嘗試構建一個應用程序,它接受來自用戶的輸入並對其進行編碼並將其顯示回用戶輸入中的user.every字母應該由輸出中的另一個字母替換。這與字典的鍵/值一起使用。在我的代碼中,只能使用一個字母,如果再輸入一個字母,那麼它會打破。任何想法都將不勝感激,能夠在輸入中鍵入一個單詞,並在輸出中打印出相反的字母作爲單詞。python的字典鍵/值
def getInput():
userInput = input("Enter String to encode: ")
return userInput
def displayOutput(userInput, encoding):
matchFound = False
for key in encoding:
if (key == userInput):
print("Encoded message", encoding[key])
matchFound = True
break
if matchFound == False:
print("***Error*** No Match Found!")
def main():
encoding ={"a": "b", "b": "c", "c": "d", "d": "e", "e": "f", "f": "g", "g": "h", "h": "i", "i": "j", "j": "k", "k": "l", "l": "m", "m": "n", "n": "o", "o": "p", "p": "q", "q": "r", "r": "s", "s": "t", "t": "u", "u": "v", "v": "w", "w": "x", "x": "y", "y": "z", "z": "a", " ": "-"}
userInput = getInput()
displayOutput(userInput, encoding)
main()
你能更詳細的不是 「它打破」? –
您需要遍歷輸入字符串中的字符。 – molbdnilo
另外,爲什麼不'如果編碼user_input,並避免'編碼循環密鑰? –