Created
March 11, 2012 20:44
-
-
Save muratcorlu/2018156 to your computer and use it in GitHub Desktop.
Python ile TC Kimlik numara doğrulama
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding=utf-8 | |
| """ | |
| Kurallar: | |
| * 11 hanelidir. | |
| * Her hanesi rakamsal değer içerir. | |
| * İlk hane 0 olamaz. | |
| * 1. 3. 5. 7. ve 9. hanelerin toplamının 7 katından, 2. 4. 6. ve 8. hanelerin toplamı çıkartıldığında, elde edilen sonucun 10'a bölümünden kalan, yani Mod10'u bize 10. haneyi verir. | |
| * 1. 2. 3. 4. 5. 6. 7. 8. 9. ve 10. hanelerin toplamından elde edilen sonucun 10'a bölümünden kalan, yani Mod10'u bize 11. haneyi verir. | |
| Kurallar http://www.kodaman.org/yazi/t-c-kimlik-no-algoritmasi adresinden alınmıştır. | |
| """ | |
| def isValidTCID(value): | |
| value = str(value) | |
| # 11 hanelidir. | |
| if not len(value) == 11: | |
| return False | |
| # Sadece rakamlardan olusur. | |
| if not value.isdigit(): | |
| return False | |
| # Ilk hanesi 0 olamaz. | |
| if int(value[0]) == 0: | |
| return False | |
| digits = [int(d) for d in str(value)] | |
| # 1. 2. 3. 4. 5. 6. 7. 8. 9. ve 10. hanelerin toplamından elde edilen sonucun | |
| # 10'a bölümünden kalan, yani Mod10'u bize 11. haneyi verir. | |
| if not sum(digits[:10]) % 10 == digits[10]: | |
| return False | |
| # 1. 3. 5. 7. ve 9. hanelerin toplamının 7 katından, 2. 4. 6. ve 8. hanelerin toplamı çıkartıldığında, | |
| # elde edilen sonucun 10'a bölümünden kalan, yani Mod10'u bize 10. haneyi verir. | |
| if not (((7 * sum(digits[:9][-1::-2])) - sum(digits[:9][-2::-2])) % 10) == digits[9]: | |
| return False | |
| # Butun kontrollerden gecti. | |
| return True |
The code works but I think there is a mistake on calculation here:
(((7 * sum(digits[:9][-1::-2])) - sum(digits[:9][-2::-2])) % 10) == digits[9]
(digits[:9][-1::-2]) this bit sums the wrong numbers causing incorrectly validate some ID numbers..
Here is fully tested and working version;
def is_valid_tckn(t):
t = str(t)
# length must be 11
if len(t) != 11:
return False
# digits only
if not t.isdigit():
return False
# first cannot be 0
if t[0] == '0':
return False
digits = list(map(int, t))
# 10th digit check:
# (d1+d3+d5+d7+d9)*7 - (d2+d4+d6+d8) mod 10 == d10
sum_odd = digits[0] + digits[2] + digits[4] + digits[6] + digits[8]
sum_even = digits[1] + digits[3] + digits[5] + digits[7]
digit10_calc = ((sum_odd * 7) - sum_even) % 10
if digit10_calc != digits[9]:
return False
# 11th digit check:
# sum(d1..d10) mod 10 == d11
if (sum(digits[:10]) % 10) != digits[10]:
return False
return True
Here is the test:
10000000146 is a valid Turkish ID:
print(isValidTCID("10000000146"), is_valid_tckn("10000000146"))
Result: Both True
12345678901 is an invalid Turkish ID:
print(isValidTCID("12345678901"), is_valid_tckn("12345678901"))
Your function still True whilst my one returns False
So, you may want to fix it. Regards..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
305