猿问

用于验证日期和性别代码的Python代码

我正在创建一个 python 程序来检查 7 位社会安全号码,以以下格式告诉该人的出生日期和性别:(我无法使用 datetime 函数)。


例如,当用户输入社会安全号码(例如1504084)时,程序会说出他们的出生日期和性别。最后一个数字“4”表示该人是2000年以后出生的女性(1:1999年之前出生的男性,2:1999年之前出生的女性,3:2000年之后出生的男性,4:2000年之后出生的女性)。


[期望输出]


Enter your social registration number

>>> 1504084

You were born on **2015/04/08**

You are a female born after 2000

然而,上面的粗体行始终显示为“1915/04/08”。谁能检查我下面的代码并告诉我我做错了什么?提前致谢!


这是我的代码:


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



杨__羊羊
浏览 137回答 3
3回答

qq_遁去的一_1

我相信你的问题是if G == 1 or 2:。这就是说它是 G 或 2。我认为你需要的是if G == 1 or G == 2。def yearcheck(G, YY):&nbsp;&nbsp; &nbsp; if G == 1 or G == 2:&nbsp; &nbsp; &nbsp; &nbsp; return 1900 + YY&nbsp; &nbsp; elif G == 3 or G == 4:&nbsp; &nbsp; &nbsp; &nbsp; return 2000 + YY&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; return False

Cats萌萌

您的 if 语句中有一些错误。更改所有这些语句:if&nbsp;G&nbsp;==&nbsp;1&nbsp;or&nbsp;2:到if&nbsp;G&nbsp;==&nbsp;1&nbsp;or&nbsp;G==2:或者if&nbsp;G&nbsp;in&nbsp;(1,&nbsp;2):现在,您的代码认为 2 是正确的,因此它将 19+YY 传递给结果

交互式爱情

问题是你应该写if G == 1 or G == 2 ,因为2它本身总是评估为 True;类似地对于if G == 3 or G == 4
随时随地看视频慕课网APP

相关分类

Python
我要回答