索引错误-适用于我的代码的一部分,而不适用于另一部分

西奥·皮尔森·布雷

我不断收到索引错误:

User_input is 0
Key is 0

Traceback (most recent call last):
  File "C:\Users\Theo_2\Google Drive\Computer science\Encryption and decryption work\Cipher 2\Cipher 2.5.0 alpha1.py", line 62, in <module>
    Test_algorithm(5)
  File "C:\Users\Theo_2\Google Drive\Computer science\Encryption and decryption work\Cipher 2\Cipher 2.5.0 alpha1.py", line 49, in Test_algorithm
    Encrypt(User_input)
  File "C:\Users\Theo_2\Google Drive\Computer science\Encryption and decryption work\Cipher 2\Cipher 2.5.0 alpha1.py", line 21, in Encrypt
    ref_for_output = Master_Key.index(User_input[Count]) + Master_Key.index(Key[Count])
IndexError: string index out of range

User_input为0部分正在测试

基本上,以下代码是对我编写的使用关键字加密和解密的内容的重写。这个版本还没有完成,我的所有功能都已定义,包括一个可以测试所有可能值的功能,但是我还没有启动任何接口。无论如何,我的代码如下:

import time
Master_Key = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#£$%&'()*+,-./:;?@[\\]^_`{|}~\t\n\r\x0b\x0c"
global Output
Output = ""
global Key
Key = ""
def Compatibility_check(Key):
    while Key == "":  
        print("Your key cannot be blank")
        Key = input("Please input a new key: ")
    while len(Key) > len(User_input):
        Key = Key[:-1]
    while len(Key) < len(User_input): 
        Key += (Key[Temp])
        Temp += 1

def Encrypt(User_input):
    Count = 0
    Output = ""
    while Count <= len(User_input):\
        ref_for_output = Master_Key.index(User_input[Count]) + Master_Key.index(Key[Count])
        if ref_for_output > len(Master_Key):
            ref_for_output -= len(Master_Key)
        Output += Master_Key[ref_for_output]
        Count += 1
        print(Output)

def Decrypt(User_input):
    Count = 0
    Output = ""
    while Count <= len(User_input):
        ref_for_output = Master_Key.index(User_input[Count]) - Master_Key.index(Key[Count])
        if ref_for_output < 0:
            ref_for_output += len(Master_Key)
        Count += 1
        Output += Master_Key[ref_for_output]

def Test_algorithm(Null):
    fail, Input_Counter = False, 0
    while Input_Counter <= len(Master_Key):
        User_input = Master_Key[Input_Counter]
        print("User_input is " + User_input)
        Key_Counter = 0
        Input_Counter += 1
        while fail == False:
            while Key_Counter < len(Master_Key):
                Key = Master_Key[Key_Counter]
                print("Key is " + Key)
                Encrypt(User_input)
                print("The encrypted value is " + Output)
                Decrypt(Output)
                print("The decrypted value is " + Output)
                if Output == User_input:
                    print("The encryption and decryption of " + str(User_input) + " with the key " + str(Key_Counter) + " results in " + Output)
                    print("pass")
                else:
                    print("fail")
                    print("The encryption and decryption of " + str(User_input) + " with the key " + str(Key_Counter) + " results in " + Output)
                    fail = True
                Key_Counter += 1

Test_algorithm(Nothing)

##Key = "abcdefghijklmnop"
##User_input = "12345"
##Compatibility_check(Key)
##Encrypt(User_input)
##print(Output)

这是对旧脚本的改进,该脚本可以工作,但确实可以改善。我重写它的原因是我想将加密的值输出到文件,然后根据用户请求重新读取和解密它们。希望您能提供帮助-谢谢。

诺尼卜

看起来变量User_input是字符串“ 0”,因此当Count为1时失败,因为索引从0开始并且User_input [1]是字符串的第二个字母。

您可能想通过更改以下内容来开始:

while Count <= len(User_input):

对此:

while Count < len(User_input):

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

for循环,拆分字符串,将字符串的一部分保存到新列表,IndexError:列表索引超出范围-适用于字符串的一部分,而不适用于另一部分

我的代码的哪一部分导致未定义索引错误?(PHP)

php 使用 get 值的问题。适用于页面的一部分但不适用于另一部分

CSS代码仅适用于一部分,而不适用于其他部分

Scala错误处理-如果我的Scala代码的一部分失败,我想将function()作为程序的一部分执行

我的代码的哪一部分导致了“超时错误”,为什么?

索引数组的一部分

MongoDB $ match不适用于子文档的一部分

错误时跳过脚本的一部分

根据另一个索引列表删除列表的一部分

我查询的哪一部分是错误的?UNNEST功能

输出的第一部分是正确的,然后我得到了分段错误?

PHP错误输出清除了我的html布局的一部分

MySQL-WHERE语句仅适用于SELECT语句的一部分

通过调用第一部分提取索引第二部分

用索引处的另一个序列替换序列的一部分

R根据另一个索引列表删除列表的一部分

pyomo:如何僅在集合的一部分中索引變量?

SQLite中列的一部分上的索引

如何使数据透视表的熊猫索引成为列名的一部分?

按索引位置删除字符串的一部分

使用日期时间索引的一部分联接数据框

多索引数据框的一部分上的熊猫操作

在Java中的索引之间替换字符串的一部分

如何在单词索引后获取标题的一部分 - wordpress

使用索引替换2D numpy数组的一部分

获取包含在字符向量中的名称的一部分字符的索引

熊猫只用日期时间索引替换列的一部分

索引以导航到视频的一部分