简体   繁体   English

Python脚本无法正常工作

[英]Python script isn't working

I wrote this script for a program I'm writing ever since I changed careers but running into a lot of issues with it. 自从我转行以来,我一直在为自己编写的程序编写此脚本,但是遇到了很多问题。 Supposed to take a string and encrypt it with a key. 应该采用字符串并用密钥对其进行加密。 Not sure where to begin with troubleshooting as I'm new to programming so came here for help. 由于我是编程新手,因此不确定从哪里开始进行故障排除,因此请寻求帮助。 Maybe if you can just point me in the write direction where to begin? 也许您可以只指出我的写作方向从哪里开始?

This is the error I get but looks fine. 这是我得到的错误,但看起来不错。

$ python temp.py -p "ThisIsATestOfMyCode" -k "testtest"
  File "encrypt.py", line 37
    else:
       ^

This is my code. 这是我的代码。

#!/usr/bin/env python
import sys, argparse

def encrypt(varAble1, varAble2):
    varAble1_size = len(varAble1)/float(len(varAble2))
    if str(varAble1_size).split(".")[1] == "0":
    else:
        while str(varAble1_size).split(".")[1] != "0":
            varAble1 +== "@"
            varAble1_size = len(varAble1)/float(len(varAble2))
    code = []
    varAble1 = list(varAble1)
    varAble2 = list(varAble2))
    multiply_size = int(str((varAble1_size)).spliy(".")[0]) * 8
    while varAble1 != []:
        p_varAble1 = varAble1[0:8]
        p_varAble2 = varAble2[0:8]
        temp_list = []
        for i in xrange(0,8):
            if type(p_varAble2[i]) == type(int):
                new_ct = (ord(chr(p_varAble2[i])) ^ ord(p_varAble1[0]))
            else:
            new_ct = (ord(p_varAble2[i]) ^ ord(p_varAble1[0]))
            code.append(new_ct)
            temp_list.append(new_ct)
            varAble1.pop(0)
            p_varAble1.pop(0)
            varAble2 = temp_list
       varAble2.reverse()
    code.reverse()
    varAble1 = code.reverse()
    code_text = []
    for i in code:
        hex_value = hex(i)
        if len(hex_value) != 4:
            code_text.append("0" + hex(i)[2:])
        else:
            code_text.append(hex(i)[2:])
            varAble2 += i
    code_text = "".join(code_text).upper()
    return code_text

def main():
    parser = argparse.ArgumentParser(description="Encrypt things")
    parser.add_argument("-p", "--var1",help="String to enc",metavar='<pt>', required=True)
    parser.add_argument("-k", "--var2", help="8 length key to encrypt with", metavar='<key>', required=True)
    args = parser.parse_args()
    var1 = args.var1
    var2 = args.var2
    hash = encrypt(var1, var2)
    print "[+] Encrypted using %s [+]\n%s" % (var2, hash)

if __name__ == "__main__":
    main()

The block of if str(varAble1_size).split(".")[1] == "0": is empty, so you will need to add a pass statement after it. if str(varAble1_size).split(".")[1] == "0":为空,因此您需要在其后添加pass语句。 Keef Baker is also correct in spotting that the block for else: on line 37 is not properly indented. Keef Baker也正确地发现了else:第37行没有正确缩进。

You have to indent your code after else : 您必须缩进代码, else

else:
    new_ct = (ord(p_varAble2[i]) ^ ord(p_varAble1[0]))

And as Andrew Kulpa noted, the block after 正如安德鲁·库尔帕(Andrew Kulpa)所指出的,

if str(varAble1_size).split(".")[1] == "0":

is empty, so you either need to do something in that block, or add a pass statement. 是空的,因此您要么需要在该块中执行某些操作,要么添加pass语句。

Python does not use brackets but indentation for control flow. Python不使用方括号,而是将缩进用于控制流。 In your code, the Python interpreter sees an else but no statement for it, so it throws an error. 在您的代码中,Python解释器会看到一个else,但没有声明,因此会引发错误。 See more about control flow here : https://docs.python.org/3/tutorial/controlflow.html 在此处查看有关控制流的更多信息: https : //docs.python.org/3/tutorial/controlflow.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM