简体   繁体   中英

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. Keef Baker is also correct in spotting that the block for else: on line 37 is not properly indented.

You have to indent your code after else :

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

And as Andrew Kulpa noted, the block after

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

is empty, so you either need to do something in that block, or add a pass statement.

Python does not use brackets but indentation for control flow. In your code, the Python interpreter sees an else but no statement for it, so it throws an error. See more about control flow here : https://docs.python.org/3/tutorial/controlflow.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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