Advertisement
Rodripelto

validar_nif

Dec 6th, 2021
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1.  
  2. def validar(dni):
  3.   control = {0:"T",1:"R",2:"W",3:"A",4:"G",5:"M",6:"F",8:"P",9:"D",10:"X",11:"B",12:"N",13:"J",14:"Z",15:"S",16:"Q",17:"V",18:"H",19:"L",20:"C",21:"K",22:"E"}
  4.   if len(dni)!=9:
  5.     return False
  6.   dig = dni[0].upper()
  7.   if dig == "X":
  8.     dig = "0"
  9.   elif dig == "Y":
  10.     dig = "1"
  11.   elif dig == "Z":
  12.     dig = "2"
  13.   try:
  14.     num = int(dig + dni[1:8])
  15.   except:
  16.     return False
  17.   num = num % 23
  18.   if control[num] == dni[-1].upper():
  19.     return True
  20.   else:
  21.     return False
  22.  
  23. while not validar(input("Introduzca NIF a validar: ")):
  24.   print("NIF inválido, pruebe de nuevo")
  25.  
  26. print("NIF correcto")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement