'str' object has no attribute 'append'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pyth0nnewbie
    New Member
    • Feb 2015
    • 5

    'str' object has no attribute 'append'

    Code:
    cars = ['honda' , 'ford' , 'kia' , 'toyota' , 'jaguar' , 'daimler' , 'citroen' , 'peugeot' , 'skoda']
    
    # Print the longest and the shortest words in the gunners list
    shortest=[]
    
    longest=[]
    shortest=[]
    for i in cars:
    	if len(i) > len(longest):
    		longest = i
    print longest
    		
    for i in cars:
    	shortest = cars[0]
    	if len(i) < len(shortest):
    			shortest = [i]
    	elif len(i) == len(shortest):
    		shortest = shortest.append[i]
    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!!
    Last edited by bvdet; Feb 22 '15, 03:28 PM. Reason: Please use code tags when posting code [code]....[/code]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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:
    Code:
    >>> cars = ['honda' , 'ford' , 'kia' , 'toyota' , 'jaguar' , 'daimler' , 'citroen' , 'peugeot' , 'skoda']
    >>> lenList = [len(car) for car in cars]
    >>> for car in cars:
    ... 	if len(car) == min(lenList):
    ... 		print "Shortest: %s" % car
    ... 	if len(car) == max(lenList):
    ... 		print "Longest: %s" % car
    ... 		
    Shortest: kia
    Longest: daimler
    Longest: citroen
    Longest: peugeot
    >>>

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by pyth0nnewbie
      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:
      Code:
      >>> len(list)
      Traceback (most recent call last):
        File "<interactive input>", line 1, in <module>
      TypeError: object of type 'type' has no len()
      >>>

      Comment

      • pyth0nnewbie
        New Member
        • Feb 2015
        • 5

        #4
        sorry i dont understand. I'm new to programming. What do i need to do to fix it?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          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:
          Code:
          >>> a = 1
          >>> b = 2
          >>> a+b
          3
          >>>
          So in your case, create the list and assign to a VALID identofier similar to a and b in the example.
          Code:
          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_l engths".

          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.

          Comment

          Working...