sentence = raw_input("Enter a sentence: ")
sentence = sentence.lower().split()
uniquewords = []
for word in sentence:
if word not in uniquewords:
uniquewords.append(word)
positions = [uniquewords.index(word) for word in sentence]
recreated = " ".join([uniquewords[word] for word in positions])
positions = [x+1 for x in positions]
print uniquewords
print positions
print recreated
file = open('task2file1.txt', 'w')
file.write('\n'.join(uniquewords))
file.close()
file = open('task2file2.txt', 'w')
file.write('\n'.join(positions))
file.close()
這是我的代碼到目前爲止,一切工作除了保存位置爲文本文件,該錯誤消息我得到的是保存號碼到一個文件
"file.write('\n'.join(positions))
TypeError: sequence item 0: expected string, int found"
你嘗試粘貼錯誤信息到谷歌搜索? – TigerhawkT3