[英]String slicing in python with curses
See oringinal link at https://codereview.stackexchange.com/questions/101011/mengenlehreuhr-in-python 参见https://codereview.stackexchange.com/questions/101011/mengenlehreuhr-in-python中的原始链接
So I have written a clock. 所以我写了一个时钟。 Not just any clock, the purpose of this clock is to crack Kryptos section K4 .
不只是任何时钟,此时钟的目的是破解Kryptos部分K4 。
Whilst the full source of the application is now on GitHub , the specific problem I am experiencing relates to the following method: 虽然该应用程序的完整源代码现在位于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()
The problem is described as follows. 问题描述如下。
When self.index < len(self.time_decipher.ciphertext)
(97 characters), the highlighting works as anticipated. 当
self.index < len(self.time_decipher.ciphertext)
(97个字符)时,突出显示将按预期进行。
OBKRUOX|O|GHULBSOLIFBBWFL...
However, when self.index == ~96
I see this: 但是,当
self.index == ~96
时,我看到了:
|AR|OBKRUOX...
Basically the last 2 characters are cut from the end of the printed string and dropped at the start. 基本上,最后2个字符是从打印字符串的末尾截取的,并在开头处删除。
Please can someone explain why this is happening and what I can do to overcome this? 请有人可以解释为什么会发生这种情况,我能做些什么来克服这一点?
Problem solved - There was an issue with counting. 问题已解决-计数有问题。
The method which set self.index
was being passed that information from another method meaning it was always 1 behind the value required for splicing and reset to 0 when it reached 97 (the length of the string). 设置
self.index
的方法正在从另一种方法传递该信息,这意味着它始终self.index
所需的值落后1,并在达到97(字符串的长度)时重置为0。
Because the slicing works from 1 to return, when it reached 96, the index was actually -1 and 97 was 0. 因为切片从1到返回工作,所以当切片达到96时,索引实际上为-1,而97为0。
Updated Log::__add__
method to set index as: 更新了
Log::__add__
方法,将索引设置为:
self.index = other.index if other.index > 0 else len(TimeDecipher.ciphertext) + other.index
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.