简体   繁体   中英

Convert number values into ascii characters?

The part where I need to go from the number values I obtained to characters to spell out a word it not working, it says I need to use an integer for the last part?

accept string

print "This program reduces and decodes a coded message and determines if it is a palindrome"
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:")))

conversion of text to ASCII code

result=[]
for i in string_lowercase:
    code=ord(i)
    result.append([code-offset])

conversion from ASCII code to text

text=''.join(chr(i) for i in result)
print "The decoded string is:", text.format(chr(result))

It looks like you have a list of lists instead of a list of ints when you call result.append([code-offset]) . This means later when you call chr(i) for i in result , you are passing a list instead of an int to chr() .

Try changing this to result.append(code-offset) .

Other small suggestions:

  • raw_input already gives you a string, so there's no need to explicitly cast it.
  • Your removal of special characters can be more efficiently written as:

     special_characters = '1234567890~`!@#$%^&*()_-+={[}]|\\:;'<,>.?/' string_lowercase = ''.join(c for c in string_lowercase if string not in special_characters) 

    This allows you to only have to iterate through string_lowercase once instead of per character in special_characters .

You are passing a list to chr when it only accepts integers. Try result.append(code-offset) . [code-offset] is a one-item list.

Specifically, instead of:

result=[]
for i in string_lowercase:
    code=ord(i)
    result.append([code-offset])

use:

result=[]
for i in string_lowercase:
    code=ord(i)
    result.append(code-offset)

If you understand list comprehension, this works too: result = [ord(i)-offset for i in string_lowercase]

While doing .append() to list, use code-offset instead of [code-offset] . As in later you are storing the value as a list (of one ASCII) instead of storing the ASCII value directly.

Hence your code should be:

result = []
for i in string_lowercase:
    code = ord(i)
    result.append(code-offset)

However you may simplified this code as:

result = [ord(ch)-offset for ch in string_lowercase]

You may even further simplify your code. The one line to get decoded string will be:

decoded_string = ''.join(chr(ord(ch)-offset) for ch in string_lowercase)

Example with offset as 2:

>>> string_lowercase = 'abcdefghijklmnopqrstuvwxyz'
>>> offset = 2
>>> decoded_string = ''.join(chr(ord(ch)-offset) for ch in string_lowercase)
>>> decoded_string
'_`abcdefghijklmnopqrstuvwx'

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