Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error message - help!
#1
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
Reply
#2
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
Reply
#3
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.
Reply
#4
(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)   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error message about iid from RandomizedSearchCV Visiting 2 933 Aug-17-2023, 07:53 PM
Last Post: Visiting
  Another Error message. the_jl_zone 2 943 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,682 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  understanding error message krlosbatist 1 1,856 Oct-24-2021, 08:34 PM
Last Post: Gribouillis
  Error message pybits 1 35,985 May-29-2021, 10:26 AM
Last Post: snippsat
  f-string error message not understood Skaperen 4 3,267 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  Overwhelmed with error message using pandas drop() EmmaRaponi 1 2,300 Feb-18-2021, 07:31 PM
Last Post: buran
  Winning/Losing Message Error in Text based Game kdr87 2 2,927 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  Don't understand error message Milfredo 2 1,994 Aug-24-2020, 05:00 PM
Last Post: Milfredo
  what to do about this error message deric23 0 1,884 May-15-2020, 02:49 PM
Last Post: deric23

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020