Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3:ValueError:int()的文本无效,以10为底:';0001.01100102E和#x2B;22';_Python_Python 3.x_Valueerror - Fatal编程技术网

Python 3:ValueError:int()的文本无效,以10为底:';0001.01100102E和#x2B;22';

Python 3:ValueError:int()的文本无效,以10为底:';0001.01100102E和#x2B;22';,python,python-3.x,valueerror,Python,Python 3.x,Valueerror,我是初学者,如果这是显而易见的,我很抱歉 我在这里不知所措。我一直在尝试做一个加密/解密程序,但我一直收到这个错误。我知道在这个问题上还有其他问题,但我仍然无法解决 加密机: import binascii def text_to_bits(text, encoding='utf-8', errors='surrogatepass'): bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]

我是初学者,如果这是显而易见的,我很抱歉

我在这里不知所措。我一直在尝试做一个加密/解密程序,但我一直收到这个错误。我知道在这个问题上还有其他问题,但我仍然无法解决

加密机

import binascii
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
    bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]
    return bits.zfill(8 * ((len(bits) + 7) // 8))

def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
    n = int(bits, 2)
    return int2bytes(n).decode(encoding, errors)

def int2bytes(i):
    hex_string = '%x' % i
    n = len(hex_string)
    return binascii.unhexlify(hex_string.zfill(n + (n & 1)))

#ENCRYPTION ALGORITHM
algorithm = 61913299

#ASCII ----> NUMBERS
raw = input("Enter text to encrypt:")

binary = text_to_bits(raw)
binary = int(binary)
algorithm = int(algorithm)
encrypted = binary * algorithm
encrypted = str(encrypted)
print(encrypted)

print("Done")
解密程序:

import sys
import time

def to_bin(string):
    res = ''
    for char in string:
        tmp = bin(ord(char))[2:]
        tmp = '%08d' %int(tmp)
        res += tmp
    return res

def to_str(string):
    res = ''
    for idx in range(len(string)/8):
        tmp = chr(int(string[idx*8:(idx+1)*8], 2))
        res += tmp
    return res

incorrectpasswords = 0
password=("password")
originpassword = password
x = 1
algorithm = 61913299

while x==1:
    passwordattempt =input("Enter Password:")
    if passwordattempt == password:
        print("Correct")
        x = 2

    if passwordattempt!= password:
        print("Incorrect")
        incorrectpasswords = incorrectpasswords + 1
    if incorrectpasswords > 2:
        if x == 1:
            print("Too many wrong attempts, please try again in one minute.")
            time.sleep(60)


encrypted = input("Enter numbers to unencrypt:")

encrypted = int(encrypted)

one = encrypted / algorithm
size = sys.getsizeof(one)
one = str(one).zfill(size + 1)
one = int(one)
unencrypted = to_str(one)

x = unencrypted

对于二进制和文本以及文本和二进制之间的转换,我使用了我在网上找到的一些代码。

我认为您的代码不起作用,因为:

one = encrypted / algorithm
生成一个浮动

要将字符串转换回数字,您应该应用

eval(one)

而不是

int(one)
(也可以在应用float或eval后将其转换为int) 或者,您可以使用整数除法
/
而不是
/
,通过将除法的小数结果放在地板上,使
成为一个
类型
int
,但我不确定这是否是您正在寻找的行为

python 3 shell中的示例:

>>> import sys
>>> one = 15/25
>>> size = sys.getsizeof(one)
>>> one = str(one).zfill(size+1)
>>> one
'00000000000000000000000.6'
>>> type(one)
<class 'str'>
>>> one = eval(one)
>>> one
0.6
>>> type(one)
<class 'float'>
导入系统 >>>一=15/25 >>>size=sys.getsizeof(一个) >>>一个=str(一个).zfill(大小+1) >>>一个 '00000000000000000000000.6' >>>类型(一) >>>一=评估(一) >>>一个 0.6 >>>类型(一)
是否显示错误在哪一行?@MatthewCiaramitaro是的,对不起。第49行。”one=int(one)'python3中的
/
或“true division”运算符与早期的Python版本不同,即使使用
int
调用,也会返回浮点值。如果它适合您的算法,您可以使用
/
或“floor division”来代替它,只要输入正确,它将返回一个
int
。当我这样做时,我得到一个错误:TypeError:eval()arg 1必须是字符串、字节或代码对象。您在将其转换为字符串后做了吗?或者您之前做过吗?应该应用它,而不是
one=int(one)
我已经添加了上面python shell中的示例行为
>>> import sys
>>> one = 15/25
>>> size = sys.getsizeof(one)
>>> one = str(one).zfill(size+1)
>>> one
'00000000000000000000000.6'
>>> type(one)
<class 'str'>
>>> one = eval(one)
>>> one
0.6
>>> type(one)
<class 'float'>