Home > OS >  How do I make a process that checks if the user only entered numbers and floating points?
How do I make a process that checks if the user only entered numbers and floating points?

Time:01-02

i am doing a process that see what in your cpf, and your cpf needs to have 14 characters, like that "111.111.111-11", but even if i put "pppppppppppppp", the user can pass

i tried this

pedro = True
while pedro:
        cpff =input('Digite seu cpf aqui: ')
        
        if len(cpff) > digitos:
            
            print('cpf inválido')
        elif len(cpff) < digitos:
            print('cpf inválido')
        elif len(cpff) == digitos:
            find = input('O que desejas encontrar em seu cpf? ')
            break

but this is a faulty method, so after that i tried this

digitos = 14
pedro = True
while pedro:
        cpff =input('Digite seu cpf aqui: ')
        cpff = float
        if cpff != float:
            print('Digite apenas números e pontos')
        cpff = str
        if len(cpff) > digitos:
            
            print('cpf inválido')
        elif len(cpff) < digitos:
            print('cpf inválido')
        elif len(cpff) == digitos:
            find = input('O que desejas encontrar em seu cpf? ')
            break

but still going wrong, and this gave me this error "object of type 'type' has no len()" what i do?

CodePudding user response:

I would use re.search along with a regex pattern:

import re

while True:
    cpff = input('Digite seu cpf aqui: ')
    if re.search(r'^\d{3}\.\d{3}\.\d{3}-\d{2}$', cpff):
        break
    else:
        print('cpf inválido')

CodePudding user response:

There are other better approaches to solving the problem statement but I guess they would not be easy for you to understand. I have written the code which would be easiest to understand.

# check if CPF is in correct format: "111.111.111-11"

# Indexs            0 1 2 3 4 5 6 7 8 9 10 11 12 13
# cpf characters    1 1 1 . 1 1 1 . 1 1 1  -  1  1

entrada_recibida = False

while not entrada_recibida:
    cpf = input('Digite seu cpf aqui:')
    formato_correcto = True

    if len(cpf) != 14:
        formato_correcto = False
    if cpf[3]!= '.' or cpf[7]!= '.':
        formato_correcto = False
    if cpf[11] != '-':
        formato_correcto = False
    '''
    print(cpf[0:3])
    print(cpf[4:7])
    print(cpf[8:11])
    print(cpf[12:])
    '''
    if not cpf[0:3].isdigit() or not cpf[4:7].isdigit() or not cpf[8:11].isdigit() or not cpf[12:].isdigit():
        formato_correcto = False

    if not formato_correcto:
        print('Formato Incorrecto')
    else:
        entrada_recibida = True
        print('Formato Correcto')

If you have trouble understanding the last if condition, just uncomment the four print statements.

Output:

Digite seu cpf aqui:111.111.111-111
Formato Incorrecto
Digite seu cpf aqui:111.111.111.11
Formato Incorrecto
Digite seu cpf aqui:111.A11.111-11
Formato Incorrecto
Digite seu cpf aqui:999.999.999-99
Formato Correcto

CodePudding user response:

You need pythons standard regex module

import re

while True:
    cpff = input('Digite seu cpf aqui: ')
    if re.search(r'(\d[0-9] (.|-)){3}\d[0-9]$', cpff): 
        print('Válido') 
        break
    else: print('cpf inválido')

Explanation :

r'(\d[0-9] (.|-)){3}\d[0-9] $'

re.search(r'') - r is reading it as a raw string
\d[0-9]        - we select digits with range 0 - 9 only
(.|-)          - followed by a . or -
(\d[0-9] ){3}  - the whole sequence we search 3 times
\d[0-9] $       - then at the end we find 2 digits whose range is 0 to 9

CodePudding user response:

In the second version of your code, you are assigning the value float to the variable cpff instead of converting the user input to a float. This is causing the error you are seeing, because float is a type and does not have a length.

Here's one way you can fix your code:

digitos = 14
pedro = True
while pedro:
        cpff = input('Digite seu cpf aqui: ')
        if not cpff.isdigit() or '.' in cpff:
            print('Digite apenas números e pontos')
        elif len(cpff) > digitos:
            print('cpf inválido')
        elif len(cpff) < digitos:
            print('cpf inválido')
        elif len(cpff) == digitos:
            find = input('O que desejas encontrar em seu cpf? ')
            break

This code first checks if the user input is a digit (using the isdigit method) and if it contains a period (using the in operator). If either of these conditions is true, it prints an error message. If the input is valid, it checks if it is the correct length and prompts the user for the information they want to find in their CPF.

This should fix the error you were seeing and allow your code to correctly validate the user's input.

  • Related