0

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")
              else:
                   print("Is under 21")
     recordnum = recordnum + 1

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

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

4
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by woooee
0

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

0

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(":")

Edited by woooee: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.