如何修复 ** 或 pow 不支持的操作数类型:'list' 和 'int'

how to fix unsupported operand type(s) for ** or pow: 'list' and 'int'

我想加密 s 的十进制值,但我得到错误 unsupported operand type(s) for ** or pow: 'list' and 'int'。如何修复它以获得十进制输出?

#Hexadecimal to decimal
def hex_to_decimal(hex_str):
    decimal_number = int(hex_str, 16)
    return decimal_number
                 
s = "66a9b2d0b1baf7932416c65a28af3c89"    

decimal = hex_to_decimal(s)
print("s in decimal: ", decimal)


#Encryption
e = 79
n = 3220

def mod(x,y):
        if (x<y):
            return x
        else:
            c = x%y
            return c
    
def enk_blok(m):
    decimalList = [int(i) for i in str(m)]
    cipher = []
    for i in decimalList:
        cipherElement = mod(decimalList**e, n)
        cipher.append(cipherElement)
    return ''.join(cipher)

c = enk_blok(decimal)
print("The result: ", c)

看起来你在 for 循环中写了 decimalList 而不是 i:

def enk_blok(m):
    decimalList = [int(i) for i in str(m)]
    cipher = []
    for i in decimalList:
        cipherElement = mod(decimalList**e, n) # replace decimalList with i
        cipher.append(cipherElement) 
    return ''.join(cipher)

此外,这一行 return ''.join(cipher) 会引发错误,因为 cipherElement 是一个整数,而 join 方法不适用于整数列表,您应该将 cipherElementstr.