Skip to content

Instantly share code, notes, and snippets.

@monroy95
Created March 7, 2019 23:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save monroy95/cfd055f84c57c11be49604782c318c6c to your computer and use it in GitHub Desktop.
Save monroy95/cfd055f84c57c11be49604782c318c6c to your computer and use it in GitHub Desktop.
Script para validar NIT Guatemala. Python
def validar_nit(nit):
'''Funcion para validar NIT'''
# Elimina espacios en blanco
nit_n = nit.replace(' ', '')
# Elimina el guion del nit
nit_ok = nit_n.replace('-', '')
# Base para multiplicar
base = 1
# Guarda el digito validador, el ultimo
dig_validador = nit_ok[-1]
# Guarda el resto de numeros para sumar
dig_nit = list(nit_ok[0:-1])
# Reverse invierte el orden de los digitos del original
# El array inverso se refleja al original
dig_nit_rev = dig_nit.reverse() # None
try:
suma = 0
# Por cada numero del nit en inversa
for n in dig_nit:
base += 1
suma += int(n) * base
# Guarda el residuo
resultado = suma % 11
comprobacion = 11 - resultado
if suma >= 11:
resultado = suma % 11
comprobacion = 11 - resultado
if comprobacion == 10:
if dig_validador.upper() == 'K':
return True
elif comprobacion == int(dig_validador):
return True
else:
return False
except:
return False
if __name__ == '__main__':
print('Validador NIT')
nit = str(input('Ingresa el NIT: \n'))
print(validar_nit(nit))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment