1

I am creating a python program to check for a 7-digit social security number to tell the person's birthdate and gender in the format below: (I cannot use the datetime function).

For example, when the user enters a social security number(ex.1504084), the program will say their birth date and their gender. The last number '4' shows that the person is a female born after 2000(1: male born before 1999, 2: female born before 1999, 3: male born after 2000, 4: female born after 2000).

[Desired Output]

Enter your social registration number
>>> 1504084
You were born on **2015/04/08**
You are a female born after 2000

However, the line above in bold keep coming out as '1915/04/08.' Could anyone check my code below and tell me what I did wrong? Thanks in advance!

Here's my code:

YY = int(SSN[0:2])
MM = int(SSN[2:4])
DD = int(SSN[4:6])
G = int(SSN[6])

def yearcheck(G, YY): #년도 4자리수로 변환, G가 1,2일경우 1900~1999, 3,4일 경우 2000 이후
    if G == 1 or 2:
        return 1900 + YY
    elif G == 3 or 4:
        return 2000 + YY
    else:
        return False

#윤년인지 확인
def leapyear(YYYY):
    if YYYY % 4 == 0:
        return True
    else:
        return False

def monthcheck(MM):
    if (MM > 0) and (MM < 13):
        return True
    else:
        return False

def daycheck(SSN):
    mgroup1 = [1,3,5,7,8,10,12]
    mgroup2 = [4,6,9,11]
    for m in mgroup1: #1-31일 사이
        if MM == m:
            if DD >=1 and DD <= 31:
                return True
            else:
                return False
    for m in mgroup2: #1-30일 사이 확
        if MM == m:
            if DD >=1 and DD <=30:
                return True
            else:
                return False
    if MM == 2:
        # 윤년여부로 나누어 확인(윤년은 1-29일 사이, 윤년아닐경우 1-28일사이)
        if leapyear(YYYY)==True:
            if DD >=1 and DD <=29:
                return True
            else:
                return False
        else:
            if DD >=1 and DD <=28:
                return True
            else:
                return False

def valid_date_check(YY, MM, DD, G):
    if yearcheck(G,YY) != False:
        if monthcheck(MM)==True:
            if daycheck(DD)==True:
                return True
            else:
                False
        else:
            False
    else: False

def SSN_print(YY,MM,DD,G,strdate):
    if valid_date_check(YY,MM,DD,G)==True:
        if SSN[6] == "1":
            print("You were born on", strdate)
            print("You are a male born between (1900-1999)")
        elif SSN[6] == "2":
            print("You were born on", strdate)
            print("You are a female born between (1900-1999)")
        elif SSN[6] == "3":
            print("You were born on", strdate)
            print("You are a male born after 2000")
        elif SSN[6] == "4":
            print("You were born on", strdate)
            print("You are a female born after 2000")
        else:
            print('Invalid entry. Please enter a valid number')
    else:
        print('Invalid entry. Please enter a valid number')


YYYY = yearcheck(G, YY) #년도 4자리수
strYYYY= str(yearcheck(G, YY)) #최종표기용 string 년도
strdate = strYYYY+"/"+SSN[2:4]+"/"+SSN[4:6]
SSN_print(YY,MM,DD,G,strdate)


Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • What is your desired output? – IoaTzimas Oct 01 '20 at 15:12
  • Hi, my desired output is mentioned in [Desired output] above, but what I get wrong is the 4-digit year mentioned in the output. – Angela Choi Oct 01 '20 at 15:16
  • 1
    @AngelaChoi don't do `if G == 1 or 2:` (it's always `True`). Do `if G == 1 or G == 2:` or `if G in [1, 2]:` instead – Tibebes. M Oct 01 '20 at 15:21
  • 1
    `if G == 1 or 2:` doesn't work as you may expect, use `if G == 1 or G == 2:` – Michael Butscher Oct 01 '20 at 15:23
  • 1
    also see: https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value – Wups Oct 01 '20 at 15:26
  • Also it looks like your SSN is a string of 6 characters so ```G = int(SSN[6])``` shouldn't have worked. If it's the last character you're checking better of to use ```int(SSN[-1])``` – Mark Oct 01 '20 at 15:28

3 Answers3

2

There are some errors in your if statements. Change all these statements:

if G == 1 or 2:

to

if G == 1 or G==2:

or

if G in (1, 2):

As it is now, your code considers 2 as correct so it passes 19+YY to result

IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
2

The problem is that you should write if G == 1 or G == 2 , because 2 on its own will always evaluate to True;

and similarly for if G == 3 or G == 4

Peter Prescott
  • 768
  • 8
  • 16
2

I believe your problem is if G == 1 or 2:. What that is saying is it is G or 2. I think what you need is if G == 1 or G == 2.

def yearcheck(G, YY): 
    if G == 1 or G == 2:
        return 1900 + YY
    elif G == 3 or G == 4:
        return 2000 + YY
    else:
        return False
Mark
  • 3,138
  • 5
  • 19
  • 36