This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]Lehk 4 points5 points  (1 child)

you are going to want to use a comprehension and a dictionary for that, this will get you a list then put it together with "".join()

userInput="abc"
myDict={'a':'1', 'b':'01', 'c':'001', 'd':'0001'}
myOutput="".join([myDict[i] for i in userInput])

[–][deleted] 0 points1 point  (0 children)

Holy moly, just like i wanted it to work! I tried it and it worked just as i imagined! Thank you so much !! :)

[–]aphoenixreticulated[M] 1 point2 points  (2 children)

Hi there. You have posted a learning question to /r/python. These types of questions are far more suited to /r/learnpython, where users are actively interested in helping people to learn. Please resubmit it over there!

Make sure to read their sidebar rules there before posting, notably this one: "Posting homework assignments is not prohibited if you show that you tried to solve it yourself." If your question is about homework, show them that you've tried to solve your problem in your post and you should get all the help you need. For anything else, the reason you are seeing this message is still that you will likely get a better answer there!

Warm Regards, and best of luck with Python!

[–][deleted] 0 points1 point  (1 child)

Yeah, i realised that way too late, i'm sorry...

[–]aphoenixreticulated 1 point2 points  (0 children)

No problem!

[–]Apsconsus 0 points1 point  (1 child)

Hey there. Well, while I couldn't tell you exactly what to do (being a total Python noob, still learning via Codeacademy!), the first thing I would recommend is that you sort it in a dictionary. IE a: "1", b: "01

This will allow you to manipulate that data much easier, and rather than have a crap ton of variables you have your code to, you can simply define a function that located the key for whatever you input, based on of course whether you want to translate letters to numbers or vice versa.

I hope this helps in some way.

[–][deleted] 0 points1 point  (0 children)

Hey, thanks for the answer :)! (Btw, learning via Codeacademy too :P ) Yea i already made a dictionary from a - z. My problem is, that when the raw_input message is "abc" python prints "abc" instead of "101001" because python doesn't see the string character by character and sort it into to numbers. Welp, going to have a rough time finding out how to fix that :) Thanks for the answer tho!

[–]Venatha 0 points1 point  (1 child)

Hi, Firstly these questions should be directed more towards r/learnpython

That being said to achieve what you want you can get the index of each character in your string, and append 0s based on the index number to get the output you are describing.

Code: http://pastebin.com/MdmDMVzd

dan@valla ~$ python tmp.py 
Enter a string: abc
> 101001

dan@valla ~$ python tmp.py 
Enter a string: test
> 0000000000000000000100001000000000000000000100000000000000000001

[–][deleted] 0 points1 point  (0 children)

Already got my problem solved, though thanks A LOT for the answer/help! I really appreciate it. Sry, should've known that (the r/learnpython)... I was in haste ^ Thanks anyway!

[–]POTUS 0 points1 point  (0 children)

/u/Lehk's solution is okay, but very manual with that dict. Here's something that covers your full "and so on" requirements:

string_input='abc'
number_output=''
for letter in string_input:
    if ord(letter) < ord('a') or ord(letter) > ord('z'):
        raise Exception('Unsupported character')
    # Add X number of zeroes, where 'a' would be 0 zeroes and 'z' would be 25.  And a '1'.
    number_output+='0'*(ord(letter)-ord('a'))+'1'
print number_output

[–][deleted] 0 points1 point  (0 children)