Python Forum

Full Version: Error message - help!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working on a program and this is what I have:
def reverse():
    global positions
    global uniqueWords
    uniqueWords = uniqueWords.split()
    newSentence = []
    positions = list(map(int,positions.split()))
    file3 = open("original.txt","w")
    for i in positions:
        newSentence.append(uniqueWords[i-1])
        newSentence = " ".join(newSentence)
    file3.write(newSentence)
    file3.close()
    print(uniqueWords)
    return
file1 = open("words.txt", "w")
file2 = open("positions.txt", "w")
sentence = input("What is your sentence?: ")
sentence = sentence.split()
words = sentence
positions = [0]
uniqueWords = []
for i in words:
    if i not in uniqueWords:
        uniqueWords.append(i)
uniqueWords = " ".join(uniqueWords)
file1.write(uniqueWords)
file1.close()
for count, i in enumerate(sentence): 
    if sentence.count(i) < 2:
        positions.append(max(positions) + 1)
    else:
        positions.append(sentence.index(i) + 1)    
positions.remove(0)
positions = " ".join(map(str, positions))
file2.write(positions)
file2.close()
option = input("Would you like to convert your sentence back to its original form? ")
if option in ["yEs","yeS","yES","YEs","Yes","yes","YES","y","Y"]:
    reverse()
However, I keep getting an error message which says:
Error:
Traceback (most recent call last):   File "\\HUHTA-FPS0001\Claitusers$\huhta-clait53\documents\Phillip Holmes\Programs\Task3\Task3v4.py", line 39, in <module>     reverse()   File "\\HUHTA-FPS0001\Claitusers$\huhta-clait53\documents\Phillip Holmes\Programs\Task3\Task3v4.py", line 9, in reverse     newSentence.append(uniqueWords[i-1]) AttributeError: 'str' object has no attribute 'append' >>> 
Please help
Add a print statement as follows:

    for i in positions:
        print('i: {}, type positions: {}, positions: {}'.format(i, type(positions), positions)
        newSentence.append(uniqueWords[i-1])
        newSentence = " ".join(newSentence)
That should help
newSentence starts as an empty list (line 5), however after the first iteration in the loop it becomes str:

    for i in positions:
        newSentence.append(uniqueWords[i-1])
        newSentence = " ".join(newSentence) # <-- here it becomes str
you need to take this line out of the loop. That said, this will correct that specific error, however your code has number of deficiencies, that need to be corrected.
(Mar-06-2017, 02:38 PM)Skippy Wrote: [ -> ]I am working on a program and this is what I have:
    for i in positions:
        newSentence.append(uniqueWords[i-1])
        newSentence = " ".join(newSentence)        # <-- newSentence is not a list anymore, so previous row will raise error on next word
During first iteration you overwrite your newSentence variable with a string, and a string has no append() method ...

Perhaps you dont want to concatenate words in newSentence until the end of the iteration over uniqueWords? In that case removing indentation could help (i didnt check rest of function):
    for i in positions:
        newSentence.append(uniqueWords[i-1])
    newSentence = " ".join(newSentence)