By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 985 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

'str' object has no attribute 'append'

P: 5
Expand|Select|Wrap|Line Numbers
  1. cars = ['honda' , 'ford' , 'kia' , 'toyota' , 'jaguar' , 'daimler' , 'citroen' , 'peugeot' , 'skoda']
  2.  
  3. # Print the longest and the shortest words in the gunners list
  4. shortest=[]
  5.  
  6. longest=[]
  7. shortest=[]
  8. for i in cars:
  9.     if len(i) > len(longest):
  10.         longest = i
  11. print longest
  12.  
  13. for i in cars:
  14.     shortest = cars[0]
  15.     if len(i) < len(shortest):
  16.             shortest = [i]
  17.     elif len(i) == len(shortest):
  18.         shortest = shortest.append[i]
  19. print shortest

when i try to execute the code i get the above error on the shortest = shortest.append[i] line. i'm new to programming so go easy! thanks!!
Feb 22 '15 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You only need to initialize shortest once.

You initialize a list named longest, but in the for loop you assign longest to a string.

You are mixing up your data types in the next for loop.

Calling list method append will append an object to the list in place and return None, so your assignment to shortest will get you nowhere.

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. >>> cars = ['honda' , 'ford' , 'kia' , 'toyota' , 'jaguar' , 'daimler' , 'citroen' , 'peugeot' , 'skoda']
  2. >>> lenList = [len(car) for car in cars]
  3. >>> for car in cars:
  4. ...     if len(car) == min(lenList):
  5. ...         print "Shortest: %s" % car
  6. ...     if len(car) == max(lenList):
  7. ...         print "Longest: %s" % car
  8. ...         
  9. Shortest: kia
  10. Longest: daimler
  11. Longest: citroen
  12. Longest: peugeot
  13. >>> 
Feb 22 '15 #2

bvdet
Expert Mod 2.5K+
P: 2,851
hi

i've tried your code and it makes sense. thanks very much.

i'm still getting an error though. now its telling me:

len(list) = [len(car) for car in cars]

Syntax Error:cant assign to function call.

any ideas?

thanks again

anthony
Apparently you misread my post. The list comprehension (code on the right side of the "=" character) must be assigned to an identifier. len(list) is not a valid identifier and actually returns the following when executed:
Expand|Select|Wrap|Line Numbers
  1. >>> len(list)
  2. Traceback (most recent call last):
  3.   File "<interactive input>", line 1, in <module>
  4. TypeError: object of type 'type' has no len()
  5. >>> 
Feb 23 '15 #3

P: 5
sorry i dont understand. I'm new to programming. What do i need to do to fix it?
Feb 23 '15 #4

bvdet
Expert Mod 2.5K+
P: 2,851
You are creating a list to use in determining the length of the longest and shortest strings. You must assign this list to an identifier. This is a very basic concept that you need to understand.
A simple example:
Expand|Select|Wrap|Line Numbers
  1. >>> a = 1
  2. >>> b = 2
  3. >>> a+b
  4. 3
  5. >>> 
So in your case, create the list and assign to a VALID identofier similar to a and b in the example.
Expand|Select|Wrap|Line Numbers
  1. c = [len(car) for car in cars]
Now you have access to the list by referring to c. Use a descriptive name so you can recognize its purpose like "list_of_word_lengths".

A valid identifier must start with a letter or an underscore.
a-z
A-Z
_
After that, the identifier can use a letter, digit(0-9) or underscore.
Feb 23 '15 #5

Post your reply

Sign in to post your reply or Sign up for a free account.