Thread: list.append

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    5
    Rep Power
    0

    list.append


    Im trying to add a few things to a text file. I know how to do this but when i implement it it says

    TypeError: cannot concatenate 'str' and 'int' objects

    this is my code.

    contactList.append (name+ " scored"+score)

    name is recieved in a different function, using raw_input and used as a global in the function where im doing this.
    Score is numeric feild that is declared a score = 0 then when i do something i do score+= 1.
    But it doens't like it.
    Any ideas ???
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    em dude
    It would be:
    contactList.append(name + " scored" + str(score))

    If name is a string then that
    If the score is not a string you can't add them.

    Comments on this post

    • Ricky1 disagrees : very quick reply.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    5
    Rep Power
    0
    omg y didn't i think of that lol.
    Thanks,

    1 more think, how do i do this.

    def listAllRecord():
    #begin listAllRecord
    global contactList
    for count in range (0, len(contactList)):
    print str (count) + "\t" + contactList [count]
    #end listAllRecord

    this will show all the things on the list.

    the things on the list are like.
    Matt scored 0
    Matt scored 1
    Matt scored 5
    Steve scored 0
    Ricky scored 5

    how would i do it so i could enter the person whos scores i wanted to see ??
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Thanks first time i really could help

    Okay to your other problem (you want the def to print out all the enteries)

    Em what is that contactlist[count]???

    How about you just do this:

    Code:
    def contactlist(list):
    	for item in list:
    		print item
    Hey, are you new to python., because those were the things I forgot at the start too.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    37
    Rep Power
    10
    To output only by name, you could do:

    Code:
    def list_for_name(contactList,name):
        for item in contactList:
            if name in item: print item
    then, you'd call this function with the name you're seeking and the list as arguments. hope this helps.

    Comments on this post

    • Ricky1 disagrees : thanks.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    5
    Rep Power
    0
    thanks you two.
    One more thing.
    Would it be possible to have a function that would delete all me scores ??
    from contactList
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    ya you could do

    def delete():
    del scores
    score = [] #to make a new list ready for next start
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    37
    Rep Power
    10
    i'm not sure what you mean by 'delete all me scores'. If you mean delete all scores in the list, you could just do:
    >>> contactList = []

    if you want to delete all scores of a particular person, you could do:

    Code:
    def remove_name(name,contactList):
        toRemove = []
        for item in contactList:
            if name in item:
                toRemove.append(item)
        for item in toRemove:
            contactList.remove(item)
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    5
    Rep Power
    0
    noooooo i need to delete all the info in contactList.
    so basically the textfile is empty.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    then just do my way or zones I am not sure which one is cleaner, I never really used it.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    5
    Rep Power
    0
    thanks u2 this post has been resolved.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Welcome
    I have one last question do u have a messager
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by Ricky1
    how would i do it so i could enter the person whos scores i wanted to see ??
    Ignore the other replies you've received ;-P
    When you want lookup, you should be using a dictionary:
    Code:
    scoremap = { "Matt": [0, 1, 5],
                 "Steve": [0],
                 "Ricky": [5], }
    
    def add_score(name, score):
        try:
            scoremap[name].append(score)
        except KeyError:
            scoremap[name] = [score]
    
    add_score("Mary", 4)
    add_score("Mary", 6)
    
    def output_scores(name):
        try:
            for score in scoremap[name]:
                print "%s scored %s."%(name, score)
        except KeyError:
            print "No scores for %s."%name
    
    name = raw_input("Whose scores do you want?: ")
    output_scores(name)
    
    print "----"
    
    for name in scoremap:
         output_scores(name)
         print
    Note that the use of a bare dictionary for scoremap is NOT necessarily the best option - In production code for this kind of design, I'd probably subclass dict and have add_score() and output_scores() as methods - something along the lines of:
    Code:
    class Scoremap(dict):
        def add_score(self, name, score):
            try:
                self[name].append(score)
            except KeyError:
                self[name] = [score]
    
        def output_scores(self, name):
            try:
                return "".join("%s scored %s.\n"%(name, score)
                                     for score in self[name])
            except KeyError:
                return "No scores for %s.\n"%name
    
        def __str__(self):
            return "\n".join(self.output_scores(name) 
                                 for name in self)
    
    myscores = Scoremap()
    myscores.add_score("Chris", 3)
    myscores.add_score("Chris", 4)
    myscores.add_score("Andy", 5)
    print myscores
    But a better solution would probably be to have a Student class, and have each instance of Student store and output its own scores. You'd then have a Students class to store every student...
    ... but I'll leave that as an exercise for you to complete ;-)

    --OH.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    I only answered his question

IMN logo majestic logo threadwatch logo seochat tools logo