简体   繁体   中英

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

I want to encrypt the decimal value of s, but i got Error unsupported operand type(s) for ** or pow: 'list' and 'int'. How to fix it to get the output in decimal?

#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)

Looks like you have written decimalList instead of i in for loop:

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)

Also, this line return ''.join(cipher) will raise an error, because cipherElement is an integer, and join method doesn't work with the list of integers, you should cast cipherElement to str .

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