Abhishek Kaushik
Abhishek Kaushik

Reputation: 93

AttributeError: 'str' object has no attribute 'pop'

Error trace:

C:\Users\Abhi.Abhi-PC\Desktop\PYE>ex25ex.py Traceback (most recent
call last):   File "C:\Users\Abhi.Abhi-PC\Desktop\PYE\ex25ex.py", line
41, in <module>     print_last_word(sentence)   File
"C:\Users\Abhi.Abhi-PC\Desktop\PYE\ex25ex.py", line 17, in
print_last_word     word = words.pop(1) AttributeError: 'str' object
has no attribute 'pop'

Here is the code

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split()
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(1)
    print word

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

sentence="island has lush vegetation, area. However, summers are cooler than those  abundant."

break_words(sentence)
sort_words(sentence)
print_last_word(sentence)
print_last_word(sentence)
print_first_and_last_sorted(sentence)
print_first_and_last(sentence)

I am unable to figure the issue with the problem. I am working from the book "How to learn Python hard way".

Upvotes: 2

Views: 42047

Answers (4)

Anuj Masand
Anuj Masand

Reputation: 359

See pop() is not a string function so going forward you can create a function, pass n as the index of the character you need to pop out of the string as str. write below code into the popout:

def popout(str,n):
  front = str[:n]   # up to but not including n
  back = str[n+1:]  # n+1 through end of string
  return front + back

This might answer your question as per my understanding. if not, please correct me in the comment section.

Upvotes: 0

Fuji Komalan
Fuji Komalan

Reputation: 2047

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split()
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print(word)

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print(word)

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)

sentence="island has lush vegetation, area. However, summers are cooler than those  abundant."

wordList=break_words(sentence)
print('')
print('wordList:',wordList)
print('')
print('Sorted :',sort_words(wordList))
print('')
print_first_word(wordList)
print('')
print_last_word(wordList)
print('')
print_first_and_last_sorted(sentence)
print('')
print_first_and_last(sentence)

RESULT

wordList: ['island', 'has', 'lush', 'vegetation,', 'area.', 'However,', 'summers', 'are', 'cooler', 'than', 'those', 'abundant.']

Sorted : ['However,', 'abundant.', 'are', 'area.', 'cooler', 'has', 'island', 'lush', 'summers', 'than', 'those', 'vegetation,']

island

abundant.

However,
vegetation,

island
abundant.

Upvotes: 0

harfel
harfel

Reputation: 296

The problem with your code is that you ignore the return values of your functions, especially break_words, and always operate over the original input. This would fix the problem:

words = break_words(sentence)
sort_words(words)
print_first_word(words)
print_last_word(words)
print_first_and_last_sorted(sentence)
print_first_and_last(sentence)

There is another small issue that you should use words.pop(-1) to access the last item in the words list.

How could you have identified the problem from the error message. The exception is an AttributeError that states that 'str' object has no attribute 'pop'. So apparently, in the line words.pop(0), words is a string, not a list of strings, as the variable name suggests. It then only needs a quick glance to see why this variable is a string: when you call the function with print_last_word(sentence) you pass it the original data (of type str) rather than the preprocessed data that you might have intended to pass.

Upvotes: 1

Aditya Walvekar
Aditya Walvekar

Reputation: 53

What you are trying to do here, is remove a word from a string. When you try to sentence.pop(), you are trying to remove an element from an Immutable Sequence Type. Which means that you are trying to remove an element from a string, which does not make too much sense. What you should instead do, is break the string into words by words.split(' ') and then removing whichever element you want to remove from it. So a solution to replace line 17 is:

word = words.split(' ').pop(len(words.split(' '))-1)

That will remove the last word from the sentence and assign it to word.

Note: This is obviously not the most efficient solution. I broke it down to make it readable.

Hope this helps

Upvotes: 1

Related Questions