該腳本需要一個句子並對其進行編碼。代碼採用給定單詞中的每個字母,並按照單詞的長度在字母表中將其顛倒。所以「貓」變成「fwd」,「小」變成「xrfqq」,「小貓」變成「xrfgg fwd」。字母變更代碼
我想看看是否有什麼我應該做的不同,或者如果你們有一些改進建議。
#Letter altering code
alphabet = ["a","b","c","d","e","f","g","h","i","j",
"k","l","m","n","o","p","q","r","s","t",
"u","v","w","x","y","z"]
textIn= ""
while textIn.lower != 'q':
textIn = input("Type a sentence to be translated ('q' to quit).\n"\
).lower()
textOut = ""
if textIn == 'q':
break
else:
for word in textIn.split():
newWord = ""
for char in word:
if char in alphabet:
pos = alphabet.index(char)
newPos = (pos + len(word))%26
newChar = alphabet[newPos]
newWord += newChar
else:
newWord += char
textOut += newWord + " "
print(textOut)
我想你想要codereview.stackexchange.com/ – pandubear
你的具體問題是什麼?用肉眼,我可以看到你在'while textIn.lower!='q''中缺少括號。 – cdonts