简体   繁体   中英

How to convert ascii codes to text?

I need to be able to take an input, remove special characters, make all capital letters lowercase, convert to ascii code with an offset of 5 and then convert those now offset values back to characters to form a word. I keep getting TypeError: 'int' object is not iterable at the end

string=(str(raw_input("The code is:")))

#change it to lower case 
string_lowercase=string.lower()

print "lower case string is:", string_lowercase

#strip special characters

specialcharacters="1234567890~`!@#$%^&*()_-+={[}]|\:;'<,>.?/"

for char in specialcharacters:
    string_lowercase=string_lowercase.replace(char,"")

print "With the specials stripped out the string is:", string_lowercase

#input offset

offset=(int(raw_input("enter offset:")))

#converstion of text to ASCII code 

for i in string_lowercase:
    code=ord(i)
    result=code-offset

#converstion from ASCII code to text
for number in result:
    message=repre(unichr(number))
    print message

You are getting TypeError: 'int' object is not iterable at the end because you are declaring result each time over the loop as an int. result should be a list and every code appended to it over the loop, like this:

#converstion of text to ASCII code 
result = []
for i in string_lowercase:
    code=ord(i)
    result.append(code-offset)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM