2016-10-02 185 views
0

我需要能夠接受輸入,刪除特殊字符,使所有大寫字母小寫,轉換爲偏移量爲5的ascii代碼,然後將這些現在的偏移值轉換回字符以形成一個字。我不斷收到類型錯誤:「詮釋」對象不是在最後如何將ascii碼轉換爲文本?

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~`[email protected]#$%^&*()_-+={[}]|\:;'<,>.?/" 

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 
+0

'result'是一個'int'。然後你試着迭代它。我認爲你想要的是'result.append(code - offset)'。這意味着你需要將'result'參數初始化爲'list',所以'result = []'應該放在你的循環之前。 – idjaw

回答

0

你得到TypeError: 'int' object is not iterable at the end,因爲你是在聲明循環爲int每次result迭代。 result應該是一個列表,並通過循環附加到它的每個代碼,如下所示:

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