【问题标题】:Python 3: ValueError: invalid literal for int() with base 10: '0001.0110010110010102e+22'Python 3:ValueError:int() 的无效文字,基数为 10:'0001.0110010110010102e+22'
【发布时间】:2017-09-07 16:27:58
【问题描述】:

我是初学者,如果这很明显很抱歉。

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

加密器

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

对于二进制与文本、文本与二进制的转换,我使用了网上找的一些代码。

【问题讨论】:

  • 它是否说明错误在哪一行?
  • @MatthewCiaramitaro 是的,对不起。第 49 行。'one = int(one)'
  • Python 3 中的 / 或“真除法”运算符与早期 Python 版本不同,即使在使用 ints 调用时也会返回浮点数。如果它适合您的算法,您可以使用// 或“地板除法”,如果输入是,它将返回int

标签: python python-3.x valueerror


【解决方案1】:

我认为您的代码无法正常工作,因为:

one = encrypted / algorithm

产生一个浮点数

把你的字符串变成你应该应用的数字

eval(one)

float(one)

而不是

int(one)

(你也可以在应用float或eval后将其转为int) 或者,您可以通过使用整数除法 // 而不是 / 来获得它,这将使 one 类型 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'>

【讨论】:

  • 当我这样做时,我得到一个错误:TypeError: eval() arg 1 must be a string, bytes or code object
  • 转成字符串后你做的吗?还是在那之前你做过?
  • 应该应用而不是one = int(one)
  • 我已经从上面的 python shell 添加了示例行为
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-11
  • 2021-08-06
相关资源
最近更新 更多