473,412 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

'str' object has no attribute 'append'

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
4 8780
bvdet
2,851 Expert Mod 2GB
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
2,851 Expert Mod 2GB
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
sorry i dont understand. I'm new to programming. What do i need to do to fix it?
Feb 23 '15 #4
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

8
by: Roy Smith | last post by:
For the past 6-8 months, I've spent most of my time writing C++ and a little bit of Java. Both of these languages support and encourage the use of private data and explicit accessor functions,...
4
by: python newbie | last post by:
Is there anything that would get in the way of me being able to reference an object attribute, from within a list. I have: # one class here class BackupSet: fileGroupList = # ...
4
by: Arjen | last post by:
Hi, I have a class with some attributes. For example class person with name as attribute. I have add multiple persons in an arraylist. Now I need a function to get/find a person by the name...
5
by: Randall Parker | last post by:
Using Python 2.4.2 on Windows 2000 in SPE. Getting: TypeError: 'str' object is not callable on this line: TmpErrMsg1 = "State machine %s " (StateMachineName) In Winpdb 1.0.6 the...
5
by: kungfoobar | last post by:
I want to have a str with custom methods, but I have this problem: class myStr(str): def hello(self): return 'hello '+self s=myStr('world') print s.hello() # prints 'hello world'...
1
by: Traclo | last post by:
I am doing a computer science project and I have to take a string that contains numbers, double each number individually and then return the doubled integers as one large string. I tried this...
2
by: BiraRai | last post by:
def getAttributeForProperty(self,rollnumber,attribute): # attribute have the value _ward ''' If year is null then use current year. Returns the value of the attribute for the given roll number...
1
by: loren41 | last post by:
OS is Ubuntu Linux 9.10/Python Version is 2.6.4/Gui Tool is Dr. Python My program has a large html-coded string called tarr_record. When I try to open an output file and write the record to the...
1
by: imhomer | last post by:
I met this problem (Django base.py: 'str' object is not callable) when I go through a Django tutorial: photo/models.py: from django.db import models from django.contrib.auth.models import User...
1
by: gintare | last post by:
I am not able to initialize empty dictionary in python33. I have tried: D=dict() D={} Later i want to insert values using: D.setdefault(num).append(ansdb2.Rname) In both cases i am getting...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.