Python Forum
Error message - help! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error message - help! (/thread-2305.html)



Error message - help! - Skippy - Mar-06-2017

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


RE: Error message - help! - Larz60+ - Mar-06-2017

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


RE: Error message - help! - buran - Mar-06-2017

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.


RE: Error message - help! - zivoni - Mar-06-2017

(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)