0

how would i go about using strings and splits to be able to get a certain grade from three different grades for a specific person out of three in the list. This is the list. Justin:Calculus$90:Java$85:Python$88: Taylor:Calculus$73:Java$95:Python$86: Drew:Calculus$80:Java$75:Python$94: I am currently stuck with this. The best example I can find was this.

def phonebook():
  return """
Mary:893-0234:Realtor:
Fred:897-2033:Boulder crusher:
Barney:234-2342:Professional bowler:"""

def phones():
  phones = phonebook()
  phonelist = phones.split('\n')
  newphonelist = []
  for list in phonelist:
    newphonelist = newphonelist + [list.split(":")]
  return newphonelist
def findPhone(person):
  for people in phones():
    if people[0] == person:
      print "Phone number for",person,"is",people[1]

as you can see. the problem with this is it only returns both the phone number and their title. what I needed to do was to return only name and grade of a class along with the class with only 2 inputs (name,subject). here is what i have so far.

def scoreList():
 return"""
  Justin:Calculus$90:Java$85:Python$88:
  Taylor:Calculus$73:Java$95:Python$86:
  Drew:Calculus$80:Java$75:Python$94:"""
def scores():
  scores=scoreList()
  scorelist=scores.split('\n')
  newscorelist=[]
  for list in scorelist:
   newscorelist=newscorelist + [list.split(":")]
   scores.split('$')
  return newscorelist
def findScore(student,subject):
  for people in scores():
   for subject in scores():
    if people[0]==student:
     if subject[0]==subject:
      print (student,"got",score,"of the course",subject[1])

and yes, I am a novice at this. I've been searching for how to do this for days now though.

user2611288
  • 1
  • 1
  • 1
  • Does the data really look like that? Where is it coming from or is this just an exercise? – RussW Jul 23 '13 at 19:33

1 Answers1

0

I would suggest using a dictionary of dictionaries.

First create your dictionary

students = {}

now for each line, create a student

for studentdata in scores.split('\n'):
    data = studentdata.split(':')
    name = data[0]
    students[name] = {}

now add each class as a key in the student dictionary

for class_data in data[1:]:
    if class_data:
        class_name,class_score = class_data.split('$')
        students[name][class_name] = class_score

putting it all together you get:

students = {}
for studentdata in scores.split('\n'):
    data = studentdata.split(':')
    name = data[0]
    students[name] = {}
    for class_data in data[1:]:
        if class_data:
            class_name,class_score = class_data.split('$')
            students[name][class_name] = class_score

students is now an dictionary of dictionary. To find scores relating to a student now findScore is:

def findScore(student,subject):
    print "student %s got %s of the course %s" % (student,students[student][subject],subject)

it is not guaranteed that student and subject are in the dictionaries, if they are not an error will occur. To prevent this, simply check if that key is in the dictionary:

def findScore(student,subject):
    if student in students:
        if subject in students[student]:
            print "student %s got %s of the course %s" % (student,students[student][subject],subject)
        else:
            print "subject %s not found for student %s" % (subject,student)
    else:
        print "student %s not found" % (student)

Now really putting it all together you get:

scores = """Justin:Calculus$90:Java$85:Python$88:
Taylor:Calculus$73:Java$95:Python$86:
Drew:Calculus$80:Java$75:Python$94:"""

students = {}
for studentdata in scores.split('\n'):
    data = studentdata.split(':')
    name = data[0]
    students[name] = {}
    for class_data in data[1:]:
        if class_data:
            class_name,class_score = class_data.split('$')
            students[name][class_name] = class_score

def findScore(student,subject):
    if student in students:
        if subject in students[student]:
            print "student %s got %s of the course %s" % (student,students[student][subject],subject)
        else:
            print "subject %s not found for student %s" % (subject,student)
    else:
        print "student %s not found" % (student)

#do some testing
findScore("Bob","Painting")
findScore("Taylor","Painting")
findScore("Taylor","Calculus")

Some more information on dictionaries (as well as other data structures) can be found here

Matthew Wesly
  • 1,238
  • 1
  • 13
  • 14
  • thanks alot, but this returns something a little wrong. when my input was findScore("Justin","Java") it gave me "student Justin got Java of the course 85" – user2611288 Jul 23 '13 at 16:42
  • Also, there is information on formatting strings [here](http://stackoverflow.com/questions/997797/python-and-s) if you want to change what it prints. – Matthew Wesly Jul 23 '13 at 17:03
  • sorry for the late response but, it was a quick fix for me yesterday. thanks a lot. I just couldnt find examples to show me how to work the 2 splits to get different parts as a result the final def findScore was ran. – user2611288 Jul 24 '13 at 14:48