【问题标题】:Error, index out of range. What is wrong?错误,索引超出范围。怎么了?
【发布时间】:2023-02-04 07:16:26
【问题描述】:

我编写了一个 Python3 脚本来解决 picoCTF 挑战。我收到了加密标志,它是: cvpbPGS{c33xno00_1_f33_h_qrnqorrs} 从它的模式来看,我认为它是使用凯撒密码编码的。所以我写了这个脚本:

alpha_lower = ['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']
alpha_upper = ['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']
text = 'cvpbPGSc33xno00_1_f33_h_qrnqorrs '

for iterator in range(len(alpha_lower)):
    temp = ''
    for char in text:
        if char.islower():
        
            ind = alpha_lower.index(char)
            this = ind + iterator
            
            while this > len(alpha_lower):
                this -= len(alpha_lower)
                
            temp += alpha_lower[this]
            
        elif char.isupper():
            ind = alpha_upper.index(char)
            that = ind + iterator
            
            while that > len(alpha_upper):
                that -= len(alpha_upper)

            temp += alpha_upper[that]
    print(temp)

我明白错误的含义。我不明白缺陷在哪里修复。提前致谢。

对不起这里是错误:

Desktop>python this.py 
cvpbPGScxnofhqrnqorrs  
dwqcQHTdyopgirsorpsst
exrdRIUezpqhjstpsqttu
Traceback (most recent call last):
File "C:\Users\user\Desktop\this.py", line 18, in <module>
temp += alpha_lower[this]
IndexError: list index out of range   

【问题讨论】:

  • 请显示错误的完整回溯。
  • this &gt;= len(alpha_lower)that &gt;= len(alpha_upper)
  • 回溯向我们展示了失败的行。
  • 而不是那个循环,使用模数:this = (ind + iterator) % len(alpha_lower)
  • 我认为稍加调试就会产生奇迹。 this 是否定的?

标签: python python-3.x algorithm


【解决方案1】:

我应该使用模数。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    相关资源
    最近更新 更多