对于带有基数为10的int(),文字无效:''只有数字的错误

时间:2018-04-16 18:03:36

标签: python python-3.x integer

我知道错误是什么,但如果没有其他字符,为什么会显示此错误?我知道这个问题已被提出,但我找不到理由。

错误行代码:

Traceback (most recent call last):
File "C:/Users/tudor/Documents/python/custom cypher/cypher.py", line 179, in 
<module>
array0, array1, array2 = (int(crypt_array0) // array0_key), 
(int(crypt_array1) // array1_key), (int(crypt_array2) // array2_key)
ValueError: invalid literal for int() with base 10: ''

完整代码:

import random
import itertools
print ('''
         READ THIS BEFORE USE

    This encryption can ONLY use ASCII
    this contains most/all of the 
    keys on your keyboard, this
    includes capital letters

    The key supports around 100000 digits.

    The hard encoding adds three more
    layers of security. You will have to
    copy your new key.

    ''')

hard_encoding_1 = {
'0': '8',
'1': '4',
'2': '3',
'3': '7',
'4': '1',
'5': '9',
'6': '5',
'7': '6',
'8': '2',
'9': '0',
}

hard_encoding_2 = {
'0': '5',
'1': '9',
'2': '0',
'3': '1',
'4': '4',
'5': '3',
'6': '7',
'7': '2',
'8': '6',
'9': '8',
}

hard_encoding_3 = {
'0': '2',
'1': '5',
'2': '7',
'3': '4',
'4': '9',
'5': '8',
'6': '1',
'7': '0',
'8': '3',
'9': '6',
}

hard_encoding_4 = {
'0': '1',
'1': '3',
'2': '5',
'3': '8',
'4': '0',
'5': '7',
'6': '6',
'7': '2',
'8': '9',
'9': '4',
}


def randint_generator(n):
    key_generate = ''
    for b in range(n):
        b = random.randint(0, 9)
        key_generate = key_generate + str(b)
    return 'copy this key: ' + key_generate


def setnumber(x, y, str_or_int):
    n = len(str(x))
    z = str(x)
    while n != y:
        z = '0' + z
        n = len(z)
    if str_or_int == str:
        return str(z)
    if str_or_int == int:
        return int(z)


while True:
    What_operation = int(input('''
        Do you want to:
        soft:
            decrypt(0)
            encrypt(1)
        hard:
            decrypt(3)
            encrypt(4)
        generate a key:
            100-digit(5)
            500-digit(6)
            1,000-digit(7)
            10,000-digit(8)
            1,000,000-digit(9)
            '''))


if What_operation == 1:
    text = input('What is your text you want to convert? ')
    New_text = bin(int.from_bytes(text.encode(), 'big'))
    key = int(input('What is your key you want to use?'))
    cyphered_text = int(New_text[2:]) * key
    print ('copy this text: ', cyphered_text)

if What_operation == 0:
    numbers = input('What is your string you want to convert? ')
    key = int(input('What is your key? '))
    New_text = '0b' + str(int(numbers) // key)
    encoded = int(New_text,2)
    decoded = encoded.to_bytes((encoded.bit_length() + 7) // 8,                 
'big').decode()
    print ('Here is your message: ' + decoded)

if What_operation == 4:
    text = input('What is your text you want to convert? ')
    New_text = bin(int.from_bytes(text.encode(), 'big'))
    key = int(input('What is your key you want to use?'))
    key_2 = random.randint(1,4)
    cyphered_text = New_text[2:]
    hard_cyphered_text = ''
    cyphered_text = int(cyphered_text) * key
    for i in str(cyphered_text):
        if key_2 == 1:
            hard_cyphered_text = hard_cyphered_text + hard_encoding_1[i]
        if key_2 == 2:
            hard_cyphered_text = hard_cyphered_text + hard_encoding_2[i]
        if key_2 == 3:
            hard_cyphered_text = hard_cyphered_text + hard_encoding_3[i]
        if key_2 == 4:
            hard_cyphered_text = hard_cyphered_text + hard_encoding_4[i]
    x_cyphered_text = str(hard_cyphered_text)
    array0 = ''
    array1 = ''
    array2 = ''
    count = 0
    for i in x_cyphered_text:
        if count % 3 == 0:
            array0 += i
        count += 1
    count = 0
    for i in x_cyphered_text:
        if count % 3 == 1:
            array1 += i
        count += 1
    count = 0
    for i in x_cyphered_text:
        if count % 3 == 2:
            array2 += i
        count += 1
    array0_key, array1_key, array2_key = random.randint(1, 99999999), 
    random.randint(1, 99999999), random.randint(1, 99999999)
    final_array0, final_array1, final_array2 = int(array0) * array0_key,                 
    int(array1) * array1_key, int(array2) * array2_key
    len_array0, len_array1, len_array2 = str(len(str(final_array0))), 
    str(len(str(final_array1))), str(len(str(final_array2)))
    full_key = str(key) + str(key_2) + setnumber(len_array0, 6, str) + 
    setnumber(len_array1, 6, str) + setnumber(len_array2, 6, str) + 
    setnumber(array0_key, 8, str) + setnumber(array1_key, 8, str) + 
    setnumber(array2_key, 8, str)
    complete_cyphered_text = str(final_array0) + str(final_array1) + 
    str(final_array2)
    print('copy this text: ', complete_cyphered_text)
    print('Copy your key: ', full_key)

if What_operation == 3:
    numbers = input('What is your string you want to convert? ')
    key = input('What is your given key? ')
    key_2 = int(key[-43:-42])
    array2_key, array1_key, array0_key = int(key[-8:]), int(key[-16:-8]),     
    int(key[-24:-16])
    len_array2, len_array1, len_array0 = int(key[-30:-24]), 
    int(key[-36:-30]), int(key[-42:-36])
    n0, n1, n2 = len_array0, len_array1, len_array2
    crypt_array0, crypt_array1, crypt_array2 = numbers[:n0], numbers[n0:n1], 
    numbers[n0 + n1:]
    array0, array1, array2 = (int(crypt_array0) // array0_key), 
    (int(crypt_array1) // array1_key), (int(crypt_array2) // array2_key) ###
    numbers_noncrypt = ''.join(''.join(x) for x in 
    itertools.zip_longest(str(array0), str(array1), str(array2), 
    fillvalue=''))
    key = int(str(key)[:-43])
    encoded_text = ''
    decoding = {}
    if key_2 == 1:
        decoding = {a: b for b, a in hard_encoding_1.items()}
    if key_2 == 2:
        decoding = {a: b for b, a in hard_encoding_2.items()}
    if key_2 == 3:
        decoding = {a: b for b, a in hard_encoding_3.items()}
    if key_2 == 4:
        decoding = {a: b for b, a in hard_encoding_4.items()}
    for i in str(numbers_noncrypt):
        encoded_text = encoded_text + decoding[i]
    encoded_text = '0b' + str(int(encoded_text) // key)
    encoded = int(encoded_text, 2)
    decoded = encoded.to_bytes((encoded.bit_length() + 7) // 8, 
    'big').decode()
    print ('Here is your message: ' + decoded)

if What_operation == 5:
    print(randint_generator(100))
if What_operation == 6:
    print(randint_generator(500))
if What_operation == 7:
    print(randint_generator(1000))
if What_operation == 8:
    print(randint_generator(10000))
if What_operation == 9:
    print(randint_generator(1000000))

顺便说一句,//不是注释,它是python中的除法以整数输出而不是浮点数,代码看起来也很拥挤,因为行必须在多长时间内有限制这个网站。我会将###放到错误行中引用。

我尝试调试它并以不同的方式接近它,但我找不到它为什么会这样做的原因。

1 个答案:

答案 0 :(得分:1)

只是为了让你的调试工作,你正在尝试将空字符串转换为整数。要查看这出错的地方,您可能想要拆分:

array0, array1, array2 = (int(crypt_array0) // array0_key), 
(int(crypt_array1) // array1_key), (int(crypt_array2) // array2_key) ###

到:

array0 = (int(crypt_array0) // array0_key)
array1 = (int(crypt_array1) // array1_key)
array2 = (int(crypt_array2) // array2_key)  

通过这种方式,可以更轻松地了解到底出了什么问题。此外,请仅在将来发布相应的代码。