-
-
Save erikwco/95ee7831261ae19e704b11861512c8ca to your computer and use it in GitHub Desktop.
Script para validar NIT Guatemala. Python
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
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