I'm having a little trouble learning some python and I'd appreciate a little direction. One of the exercises in my books says to create a string list

list = ["a","b","c","d"]

Convert each character in the list to it's ascii equivalent and then populate that information into a new list. Here's what I have so far:

new_list=[] # new, empty list for the populating

for i in list:

That's as far as I've gotten. I can get back each character as it's ascii but everything I'd tried to pop those ascii numbers into a new, corresponding list have failed.

Thoughts appreciate,

Recommended Answers

All 6 Replies

See the code in the XOR encryption thread. My fastest solution does this to password.

First avoid 'list' as a variable name since it is the name of a builtin type. There are at least 2 ways to do it

old_list = ["a","b","c","d"]

new_list = list()
for x in old_list:

or, using 'list comprehension syntax'

old_list = ["a","b","c","d"]
new_list = [ ord(x) for x in old_list ]

Gribouillis thank you very very much. I can see now I made the mistake of trying to do the ord before the append. I played around with append but it kept telling me the variable had no append function. Alas.

Tonyjv: I read the document on XOR but I don't think XOR is what this exercise has in mind as XOR is a few chapters away. However it has discussed Exclusive OR "(a and not b) or (not a and b)".

Thank you both very much.

I was referring this code little cleaned up by other poster:http://www.daniweb.com/forums/post1251707.html#post1251707

def crypt(text, password):
    password_length = len(password)
    password = [ord(character) for character in password]
    text = [ord(character) ^ password[index % password_length] for (index, character) in enumerate(text)]
    return ''.join([chr(character_code) for character_code in text])

Line three builds list of ascii values of password.

commented: Very helpful to poor newbies! +0

TonyJV: Oh wow that is quite nice! I did not see that, thank you for pointing it out. That actually helps a lot as I can see the logical flow through the process. I read up on XOR last night after reading your other post. There's an exercise coming up that encourages me to NOT use the XOR function but rather do encryption manually. Something about binary bit of cipher keys, etc. Should be fun.

Python is interesting but I'm slightly frustrated as I'm learning this as I go for work.

Thank you again for the response,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.