信改变代码

时间:2015-09-19 20:32:18

标签: python

这个脚本会收集一个句子并对其进行编码。该代码获取给定单词中的每个字母,并按字母的长度将其拼接在字母表中。所以" 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)

1 个答案:

答案 0 :(得分:0)

非常好。

alphabetstring.ascii_lowercase

textIn.lower应为textIn.lower()

break只能在循环中使用。你想要pass,这意味着什么都不做。

将新字符添加到新单词的循环很清楚。我喜欢。不过还有其他方法可以做到这一点,它们更加炽热,而且仍然可以清晰。