Text to Python binary

I am trying to write a binary translator - with a string as input and its binary representation as output.

And I have some difficulties, I wrote a translation for each letter in the variable, but they are variables, not strings, so I want to take the input from this match with the variable name and print the result:

a = "01000001" b = "01000010" c = "01000011" d = "01000100" # all the way to z word = input("enter here: ") print (word) 

When I run this, I enter a word and it just returns the same word, but when I write print(a) it returns 01000010 , but I cannot get it to work with the input.

Can someone tell me where I am doing something wrong?

+5
source share
2 answers

Following user comments, this is the best dictionary programming practice for these cases, you just need to fill out the letterToBin dictionary, as you can see in the example

This is a dictionary meaning that it will have a key, and a value, for example a cell phone, you have a key as a name (your mother) and a value (his cell phone):

 letterToBin = {} letterToBin = { "a" : "01000001", #Here, the key is the "a" letter, and the value, his bin transformation, the 01000001 "b" : "01000010", "c" : "01000011", "d" : "01000100" #so you need to add all the other keys you need, for example the "e" "e" : "01000101" #for example } binToLetter = {} # here I create a second dictionary, and it invert the values of the first, it meas, now the keys will be the bins, and the value the latters binToLetter = dict(zip(letterToBin.values(), letterToBin.keys())) #this code do the magic, you must understand, that only needs to feel the first dictionary, and for free, you will have the second dictionary wordOrBin = input("enter here: ") if wordOrBin in letterToBin: print(letterToBin[wordOrBin]) #here I has if you write a latter (a) or a bin(11001101) and it choose where to look the correct value else: print(binToLetter[wordOrBin]) 
+5
source

Perhaps a better solution is to use a dictionary instead of letter names as variable names:

 transDict = { "a": "01100001", "b": "01100010", "c": "01100011", "d": "01100100", # etc., etc. } text = input( "Enter your message: " ) result = "".join( [transDict[letter] for letter in text] ) print(result) 

(I also adjusted the ASCII codes - yours were in capital letters.)


Explanation
(for the longest statement):

Use "" as a delimiter (i.e. no delimiter ) to connect all the elements in the list of translated letters, where the letters get one after another from text ".

Thus, the result will be the same as if you used these commands:

 listOfCodes = [] # Starting with an empty list for letter in text: # For letter by letter in text perform 2 actions: code = transDict[letter] # - translate the letter listOfCodes.append( code ) # - append the translation to the list result = "".join( listOfCodes ) # Then join items of the list without a delimiter # (as "" is an empty string) (" " would be nicer) 
+3
source

Source: https://habr.com/ru/post/1268336/


All Articles