brianK
brianK

Reputation: 15

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)

Upvotes: 0

Views: 1041

Answers (1)

Tugay
Tugay

Reputation: 2214

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.

Upvotes: 1

Related Questions