Question

I have a file with peoples information, including date of birth, and want to be able to call the month out of dd/mm/yyyy format to find the persons name from thir month of birth. So far i have this:

def DOBSearch():
    DOBsrch = int(input("Please enter the birth month: "))
    for row in BkRdr:
        DOB = row[6]
        day,month,year = DOB.split("/")
        if DOB == month: 
            surname = row[0]
            firstname = row[1]
            print(firstname, " ",surname)
            addrsBk.close

But it returns

Please enter the birth month: 02
#(nothing is printed)
Was it helpful?

Solution

You need to compare ints with ints or strs with strs. DOBsrch is an int, but DOB is probably a str (since you use its split method).

So you need

day,month,year = map(int, DOB.split("/"))

or, at least

day,month,year = DOB.split("/")
month = int(month)

Also, as @devnull points out, you probably want to compare the month with DOBsrch, not DOB:

if DOBsrch == month: 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top