How to convert a string to this encoding?

0

I'm trying to convert text into a binary language, but it still leaves letters in the result, I would like a text to be created according to the number that each letter received:

a = 10000
c = 10011
d = 10000
e = 10000
f = 10000
g = 10000
h = 10000
i = 10000
j = 10000
l = 10000
m = 10000
n = 10000
o = 10000
p = 10000
q = 10000
r = 10000
s = 10000
t = 10000
u = 10000
v = 10000
w = 10000
x = 10000
y = 10000
z = 10000

texto = input('DIGITE UM TEXTO: ')

tam = len(texto)

for i in range(tam):
    print('{}' .format(texto[i]))

Can anyone help me by pointing out the error?

    
asked by anonymous 06.01.2018 / 19:17

2 answers

0

It's quite simple, in fact, you can follow the example of our friend Daniel Reis. Here is the code:

letter = {
    'a':'',
    'b':'',
    'c':'',
    'd':'',
    'e':'',
    'f':'',
    'g':'',
    'h':'',
    'i':'',
    'j':'',
    'k':'',
    'l':'',
    'm':'',
    'n':'',
    'o':'',
    'p':'',
    'q':'',
    'r':'',
    't':'',
    'u':'',
    'v':'',
    'x':'',
    'w':'',
    'y':'',
    'z':'',
    'r':'',
}

phrase = input('Entre com uma frase:')
text = ''

for i in phrase:
    text += letter[i]

print(text)

Basically in the code you have a dictionary, a dictionary is nothing more than a variable that assigns a key to a certain value, most used in lists that do not change their values and do not depend on an order to be queried or displayed, basically as a dictionary itself.

In FOR, you will be traversing each letter of the sentence that the user entered and including in the output variable the value corresponding to the letter in the dictionary, which would be the key.

Hugs.

    
07.01.2018 / 01:37
0

Instead of creating a variable for each letter of the alphabet, you should create a dictionary-like structure to convert the letters from one encoding to another. The iteration must also be modified. The code would look something like this:

alfabeto={'a':'1000','b':'1001','c':'1010'}

texto = input('DIGITE UM TEXTO: ')

saida=""

for letra in texto:
    saida=saida+alfabeto[letra]

print(saida)
    
06.01.2018 / 22:27