Guest User

Untitled

a guest
Jul 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. # Sample
  2.  
  3. def validate ():
  4. weights = (2,7,6,5,4,3,2)
  5. checksum_sg = ['J' ,'Z' ,'I' ,'H' ,'G' ,'F' ,'E' ,'D' ,'C' ,'B' ,'A']
  6. checksum_foreign = ['X' , 'W' , 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K']
  7.  
  8.  
  9. NRIC = input("Enter your NRIC: ")
  10.  
  11. #Length Check
  12. if len(NRIC) != 9:
  13. raise Exception('Wrong length')
  14.  
  15. #Checks first index
  16. if (NRIC[0] in ('S', 'T', 's', 't')):
  17. checksum = checksum_sg
  18. sum = 0
  19. elif (NRIC[0] in ('F', 'G', 'f', 'g')):
  20. checksum = checksum_foreign
  21. sum = 4
  22. else:
  23. raise Exception('Invalid first index')
  24.  
  25. for i in range(7):
  26. sum += weights[i]*int(NRIC[i+1])
  27.  
  28. if (NRIC[-1] != checksum[sum%11]):
  29. raise Exception('Invalid NRIC')
  30. else:
  31. print('NRIC verified')
  32.  
  33.  
  34.  
  35. if __name__ == "__main__":
  36. validate()
Add Comment
Please, Sign In to add comment