Re-wrote ex25 & got AttributeError? 4th Method

Can someone help me with the attribute error?? Just a bit confused.

What is happening is that you are trying to use a method not available for string type.
We expect the variable college to be string, a sequence of characters, but the pop() method works only if your data type is a list ( for example: >>> list = [0, 1, 2] ).

That’s why you got a AttributeError: because ‘str’ object has no attribute ‘pop’

Your first function school is returning [‘Cal’, ‘State’, ‘University’, ‘Northridge’], which means that your sentence was converted into a list data type and was stored in college. So since college is a list now, you can use the pop method on it.

But the problem is first_word_popped(college) function. Let’s see what is happening there. You are removing the first item on a list, in this case, the first word, and then you are storing that value in the college variable, and then you return it. When you do this, college will be equal to “Call”, in other words, string type.

I hope this can help you, but Zed can explain better for you.

Also, check this:
https://docs.python.org/3.6/library/stdtypes.html#str.split
https://docs.python.org/3/howto/sorting.html
https://docs.python.org/3.6/tutorial/datastructures.html

2 Likes

Hell yes! Thank you @funception

what if i made a new variable >>> college_popped = college.pop(-1)??? @funception

You can try it, but college_popped won’t work because of something called Mutability.
When you write something like list.pop(), Python will automatically do “stuff” with your list, even if you are just assigning a new variable.
Consider the following:

>>> 
>>> 
>>> list1 = [0, 1, 2, 3]
>>> list2 = list1
>>> 
>>> list1
[0, 1, 2, 3]
>>> list2
[0, 1, 2, 3]
>>> list2.pop(0)
0
>>> list2
[1, 2, 3]
>>> list1
[1, 2, 3]
>>> 
>>> 
>>> list3 = [0, 1, 2, 3, 4, 5]
>>> list4 = list3.copy()
>>> 
>>> list3
[0, 1, 2, 3, 4, 5]
>>> list4
[0, 1, 2, 3, 4, 5]
>>> 
>>> 
>>> list4.pop(0)
0
>>> list3
[0, 1, 2, 3, 4, 5]
>>> list4
[1, 2, 3, 4, 5]
>>> 

First, you could do something like first_word = college.copy(to return a shallow copy of the list) and just after that you can use the pop method without breaking the “college” list. And instead of returning college, you can return first_word without any side effect, unless you want the opposite.
So first_word.pop(0) and last_word(-1) will work now.

1 Like

Thank you so much for your aid @funception . Def helped a lot. I really appreciate the link as well read up on it. Finally got results I was looking for. It’s funny i spent all day trying to figure out that attribute error and finally did lol (after 5 hours).

cleared!

xquiin, you might want to plan your variable names a little more carefully. For the list, something like college_split for the split string, college_order for the sorted list. and for the popped elements last_elt or first_elt or whatever.

Too many similar names can make the code much more difficult to read. Personally, I often try to choose variable names that make my code read a little more like comments.

1 Like

I was just on that ha. Now I see why a lot of individuals sometimes are so careful with variable naming thank you! @austen