April 7th, 2005, 09:42 AM
-
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 ???
April 7th, 2005, 09:51 AM
-
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
April 7th, 2005, 10:03 AM
-
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 ??
April 7th, 2005, 11:16 AM
-
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.
April 7th, 2005, 12:17 PM
-
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
April 7th, 2005, 12:38 PM
-
thanks you two.
One more thing.
Would it be possible to have a function that would delete all me scores ??
from contactList
April 7th, 2005, 01:15 PM
-
ya you could do
def delete():
del scores
score = [] #to make a new list ready for next start
April 7th, 2005, 01:29 PM
-
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)
April 7th, 2005, 01:33 PM
-
noooooo i need to delete all the info in contactList.
so basically the textfile is empty.
April 7th, 2005, 01:49 PM
-
then just do my way or zones I am not sure which one is cleaner, I never really used it.
April 7th, 2005, 01:52 PM
-
thanks u2 this post has been resolved.
April 7th, 2005, 02:13 PM
-
Welcome
I have one last question do u have a messager
April 7th, 2005, 11:47 PM
-
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.
-
I only answered his question