numpy.core. _exceptions.Ufunctypeerror: ufunc 'add' Did not contain a loop with Signature matching types (dtype('<U38')

Asked

Viewed 77 times

0

i am starting in python and for my first 'big project' I decided to create a program that encrypts, with double key, a message according to the character indexes in an ascii character list. but when I’m trying to add together the matrices that represent each key is giving the following error:

numpy.core. _exceptions.Ufunctypeerror: ufunc 'add' Did not contain a loop with Signature matching types (dtype('<U38'), dtype('<U38')) -> dtype('<U38'')

I tried to add the matrices with '+' too and it didn’t work

import numpy as np
print('-=' * 20)
print('             Encriptador')
print('-=' * 20)

# definindo matriz que vai gerar a chave pública
a = np.random.randint(1, 10, size=(5, 5))

chave_publica = []

alfabeto = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',',
            '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '/', ']',
            '^', '_', '`', '{', '|', '}', '~',  'á', 'à', 'ã', 'é', 'è', 'í',
            'ì', 'ó', 'ò', 'ú', 'ù', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']


# definindo a chave pública
for k in range(0, 5):
    k = a[k, k]
    chave_publica.append(k)
chave_publica = np.array(chave_publica)

print(f''
      f'chave pública >>>>> {chave_publica}')

chave_privada = []

print('(frase deve ter mais que 5 caracteres)')

msg = (str(input('frase: ')).strip()).lower()


# a chave privada vai depender do tamanho da mensagem
for k in range(0, len(msg)):
    for j in range(0, len(msg[k])):
        letra = alfabeto.index(msg[k][j])
        chave_privada.append(letra)
chave_privada = '[' + (str(chave_privada).strip('[')).strip(']').replace(',', '') + ']'
chave_privada = np.array(chave_privada)
print(f'chave privada >>>>> {chave_privada}')

l_chave_pub = len(str(chave_publica))
l_chave_priv = len(str(chave_privada))


# definindo a chave para criptografada
if l_chave_priv > l_chave_pub:
    n = l_chave_priv // l_chave_pub
    r = l_chave_priv % l_chave_pub
    n_chave_pub = np.repeat(chave_publica, (n-1)) + ([0] * r)
    c_def = np.add(chave_privada, n_chave_pub)
    print(c_def)
else:
    n = l_chave_pub // l_chave_priv
    r = l_chave_pub % l_chave_priv
    n_chave_priv = np.repeat(chave_privada, n) + ([0] * r)
    c_def = np.add(n_chave_priv, chave_publica)

print(c_def)

  • I haven’t circled your code here, but I’m sure the problem is in the special characters. Like a, a, é, í... You’re using str() which will treat strings as ascii. I suggest OR do not use accented characters (prefer so), OR use u'Sua string acentuada'. Ao invés de inicializar a msg com input tente apenas msg = u'Message is accented'`

  • It seems that you can also change the type of the array. See this post

  • I get it, I’ll fix it and see if it works. Thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.