我正在创建一个 python 程序来检查 7 位社会安全号码,以以下格式告诉该人的出生日期和性别:(我无法使用 datetime 函数)。例如,当用户输入社会安全号码(例如1504084)时,程序会说出他们的出生日期和性别。最后一个数字“4”表示该人是2000年以后出生的女性(1:1999年之前出生的男性,2:1999年之前出生的女性,3:2000年之后出生的男性,4:2000年之后出生的女性)。[期望输出]Enter your social registration number>>> 1504084You 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 Falsedef monthcheck(MM): if (MM > 0) and (MM < 13): return True else: return Falsedef 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 Falsedef 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
3 回答
![?](http://img1.sycdn.imooc.com/5923e28b0001bb7201000100-100-100.jpg)
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
我相信你的问题是if G == 1 or 2:。这就是说它是 G 或 2。我认为你需要的是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
![?](http://img1.sycdn.imooc.com/54586431000103bb02200220-100-100.jpg)
Cats萌萌
TA贡献1805条经验 获得超9个赞
您的 if 语句中有一些错误。更改所有这些语句:
if G == 1 or 2:
到
if G == 1 or G==2:
或者
if G in (1, 2):
现在,您的代码认为 2 是正确的,因此它将 19+YY 传递给结果
添加回答
举报
0/150
提交
取消