我正在尝试编写一个脚本来回答我正在做的类(class)中的问题,因此我面临着深深的麻烦。我继续收到SyntaxError:无效的语法行138,这有点奇怪。这是我的剧本。如果有人可以解释如何解决这个问题,那就太好了。谢谢

class Message(object):

    def __init__(self, text):

        self.message_text = text
        self.valid_words = load_words(WORDLIST_FILENAME)


    def get_message_text(self):

        return self.message_text


    def get_valid_words(self):

        return self.valid_words[:]

    def build_shift_dict(self, shift):

        lc_str = string.ascii_lowercase
        uc_str = string.ascii_uppercase

        shifted_dict = {}


        for ltr in lc_str:
            if lc_str.index(ltr) + shift < 26:
                shifted_dict[ltr] = lc_str[lc_str.index(ltr) + shift]
            else:
                shifted_dict[ltr] = lc_str[lc_str.index(ltr)-26+shift]

        for ltr in uc_str:
            if uc_str.index(ltr) + shift < 26:
                shifted_dict[ltr] = uc_str[uc_str.index(ltr) + shift]
            else:
                shifted_dict[ltr] = uc_str[uc_str.index(ltr)-26+shift]

        return shifted_dict


    def apply_shift(self, shift):

        cipher = self.build_shift_dict(shift)
        ciphertext = ""

        for char in self.message_text:
            if char in cipher:
                ciphertext = ciphertext + cipher[char]
            else:
                ciphertext = ciphertext + char

        return ciphertext

最佳答案

在这两行之间:

shifted_dict = {}

for ltr in lc_str:

您有一个非ASCII字符('\xe2')。删除它。

(如果您尝试在Python解释器中加载代码,Python会告诉您这一点。)

关于python - 语法错误: invalid syntax line 138 unexpected error,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38710691/

10-12 17:03
查看更多