这个脚本会收集一个句子并对其进行编码。该代码获取给定单词中的每个字母,并按字母的长度将其拼接在字母表中。所以" cat"变成" 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)
答案 0 :(得分:0)
非常好。
alphabet
是string.ascii_lowercase
textIn.lower
应为textIn.lower()
break
只能在循环中使用。你想要pass
,这意味着什么都不做。
将新字符添加到新单词的循环很清楚。我喜欢。不过还有其他方法可以做到这一点,它们更加炽热,而且仍然可以清晰。