I need to create a code that will extract the dob from a array of string, like this finddob(["name:bob grade:a dob:1980","dob:1976 name:kate grade:c"])

def finddob(listofstrings):
      recordnum = 0
      while(recordnum < len(listofstrings)):
              yearstart = listofstrings[recordnum].find("dob:") + 4
              yearend = listofstrings[recordnum].find(yearend)
              year = int(listofstrings[yearstart, yearend])
              if(year > 1990):
                   print("Is over 21")
                   print("Is under 21")
     recordnum = recordnum + 1

but i get the error above, any help would be appreciated

Recommended Answers

use the code tags to display your code

Jump to Post

All 3 Replies

use the code tags to display your code

You try to use comma instead of colon for string slice. Why you don't use for loop to iterate input list?

The simple method is to split on the space and then on the colon. I will leave it up to you to organize the output.

test_list=["name:bob grade:a dob:1980","dob:1976 name:kate grade:c"]
for substr_1 in test_list:
    print "-"*30
    first_split =substr_1.split()
    for substr_2 in first_split:
        print substr_2.split(":")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.