5

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

And I'm having some difficulties, I wrote in variable the translation for each letter, but they are variables, not strings, so I want to take an input from that matches with the name of the variable and prints 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 to me but when I write print(a) it returns 01000010 but I can't get it to work with the input.

Can someone tell me where I'm doing something wrong?

4
  • you are printing "word" which is your input. Commented May 29, 2017 at 19:05
  • You don't describe your difficulties and you didn't ask a question. This is a Q&A site, so a questionmark somewhere in your text would be helpful to locate what you are actually asking. (E.g. If I switch to using Java will this magically work without coding?) Commented May 29, 2017 at 19:06
  • 1
    If I understood well, she wants to print the value of the variable, matching the input with the name of the variable, so if you input the "a" char, you get not the a result, but the 01000001, In my answer I think it works Commented May 29, 2017 at 19:10
  • a = "01000001" etc. - if you mean ASCII codes, yours are for capital letters (A, B, etc.) Commented May 29, 2017 at 19:37

2 Answers 2

5

Following the comments of the users, is a better practice of programming using a dictionary for these cases, you just only have to fill in the dictionary letterToBin as you can see in the example

This is a dictionary, wich means it will have a key, and a value, like a cell phone, you have the key as a name (your mother) and the value (his cellphone):

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])
Sign up to request clarification or add additional context in comments.

19 Comments

thank you so much it finally worked ive been trying for hours thank you
@ErenBiçer I have always loved to help, don't forget accept the answer, with the (✓)
Nice hack, but not a good approach. Using a dictionary for the translation table would be the canonical way.
@Matthias Yes endeed! hahaha, but see, she is new in this programming world, so she would to rewrite all her code again, and there are a lot of keys from a-z... don't you think?
Nice - but you used the name x for the user's input and she used the same name in her "coding table" (a thru z).
|
3

Probably the more correct solution is to use a dictionary instead of names of letters as names of variables:

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 corrected the ASCII codes - yours were for capital letters.)


The explanation
(for the longest statement):

"Use "" as delimiter (i.e. no delimiter) to join all items in the list of translated letters where letters are gotten one after other from the text".

So 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 Comments

MarianD! Nice one, ;) I really like it! +1
@DamianLattenero - thanks (I am not so fast as you are). Whereas your solution remember me things which I already forgot and more respected the OP original code.
@DamianLattenero - thanks - I realized that OP is probably not very experienced one, so I added some explanation to my original answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.