Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Text to Binary in Python

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?

Answer*

Draft saved
Draft discarded
Cancel
19
  • thank you so much it finally worked ive been trying for hours thank you Commented May 29, 2017 at 19:09
  • @ErenBiçer I have always loved to help, don't forget accept the answer, with the (✓) Commented May 29, 2017 at 19:11
  • 3
    Nice hack, but not a good approach. Using a dictionary for the translation table would be the canonical way. Commented May 29, 2017 at 19:24
  • @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? Commented May 29, 2017 at 19:26
  • 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). Commented May 29, 2017 at 19:54

lang-py

AltStyle によって変換されたページ (->オリジナル) /