-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidacao_cpf.py
More file actions
48 lines (38 loc) · 1 KB
/
validacao_cpf.py
File metadata and controls
48 lines (38 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import re
import random
cpf = ''
gera = False
if input('Escolha a opção: \'G\' -> gerar um CPF ou \'V\' -> validar um CPF ').lower() == 'g':
for i in range(9):
cpf += str(random.randint(0,9))
gera = True
else:
cpf = input('Digite o seu CPF:')
cpflimpo = ''
cpf_constru = ''
cpffinal = ''
numeros = range(0,10)
somabase = 0
def desc_dig(digitos,multiplicador):
soma = 0
for digito in digitos:
soma = soma + (int(digito) * multiplicador)
multiplicador -= 1
soma = soma * 10
soma = 0 if (soma % 11) > 9 else soma % 11
return str(soma)
# expressão regular - tratamento de strings
if not gera:
cpflimpo = re.sub(
r'[^0-9]',
'',
cpf
)
else:
cpflimpo = cpf
cpf_constru = cpflimpo[:9] + desc_dig(cpflimpo[:9],10)
cpffinal = cpf_constru + desc_dig(cpf_constru,11)
if gera:
print(cpffinal)
else:
print("CPF Válido!" if cpffinal == cpflimpo else "CPF Inválido...₢")