【问题标题】:String slicing in python with cursespython中带有curses的字符串切片
【发布时间】:2015-08-19 23:55:30
【问题描述】:

注意。这个问题与最初提交给 codereview 的代码有关

查看https://codereview.stackexchange.com/questions/101011/mengenlehreuhr-in-python的原始链接

所以我写了一个时钟。不只是任何时钟,这个时钟的目的是破解Kryptos section K4

虽然应用程序的完整来源现在是on GitHub,但我遇到的具体问题与以下方法有关:

    def _update(self):
        self.window.erase()
        self.window.box()
        self.window.addstr(0, 1, str(self.index).zfill(2) + ' - ' + TimeDecipher.ciphertext[0:self.index])
        self.window.addstr(1, self.cipher_offset, TimeDecipher.ciphertext[0:self.index-1], self.color)
        self.window.addstr(
            1,
            (self.cipher_offset + self.index-1),
            TimeDecipher.ciphertext[self.index-1:self.index],
            self.highlight
        )
        self.window.addstr(1,
            (self.cipher_offset + self.index),
            TimeDecipher.ciphertext[self.index:],
            self.color
        )
        self.window.addstr(2, 1, ''.join(['-' for i in range(self.width - 2)]))
        for i, item in enumerate(self.lines):
            self.window.addstr((i + 3), 1, str(item))
        self.window.refresh()

问题描述如下。

self.index < len(self.time_decipher.ciphertext)(97 个字符)时,突出显示按预期工作。

OBKRUOX|O|GHULBSOLIFBBWFL...

但是,当self.index == ~96 我看到这个时:

|AR|OBKRUOX...

基本上最后 2 个字符是从打印字符串的末尾剪掉并放在开头的。

请有人解释为什么会发生这种情况以及我可以做些什么来克服这个问题?

【问题讨论】:

    标签: python python-curses


    【解决方案1】:

    问题已解决 - 计数出现问题。

    设置 self.index 的方法正在从另一个方法传递该信息,这意味着它总是比拼接所需的值低 1,并在达到 97(字符串的长度)时重置为 0。

    因为切片是从1到return的,到96的时候,index其实是-1,97是0。

    更新了Log::__add__ 方法将索引设置为:

        self.index = other.index if other.index > 0 else len(TimeDecipher.ciphertext) + other.index
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 2014-07-21
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多