I need to inform the user if it contains a registered user with this same user name

Asked

Viewed 57 times

1

Opa, then, I need to inform to those who are using the program, if you already have any user registered with this name and inform the age of the same contains in the list that must be registered before the verification, however I’m having some difficulties with this, especially in the matter of fetching this data and printing, follows code.

opções1 = ("1 - Cadastrar novo usuário")
opções2 = ("2 - Listar usuários cadastrados")
opções3 = ("3 - Sair do sistema")
opções4 = ("4 - Busca de usuarios")
escolha = 0
CadastroUsuario = 0
Usuarios = list()
Idade = list()
separador= list()
lista = ""
valor = 0


while escolha != 4:

    print(opções)
    print(opções1)
    print(opções2)
    print(opções3)
    print(opções4)

    escolha = int(input("Qual opção deseja fazer? "))

    print(escolha)

    if escolha == 1:

        CadastroUsuario = int(input("Deseja cadastrar quantas pessoas? "))

        if CadastroUsuario <= 0:
            print('\033[31mValor Inválido! Digite inteiros maiores que "0"!\033[m')

        for c in range(1, CadastroUsuario + 1):
            Usuarios.append(input(f"Digite o nome do {c} Usuario: "))
            Idade.append(input(f"Digite a idade do {c} Usuario: "))
            separador.append(print(f"\033[1;34m=-==-==-==-= FIM DE CADASTRO DO USUARIO {c}!! =-==-==-==-=\033[m"))
            print(f'{c}')

    if escolha == 2:
        if CadastroUsuario > 0:
            print(f'0s usuários são: \033[32m{Usuarios}\033[m')
            print(f'As idades são: \033[32m{Idade}\033[m')

        elif CadastroUsuario == 0:
            print('=-=' * 10)
            print(f"\033[1;34mNão temos nenhum usuario cadastrado, para cadastrar um usuário, digite 1!!\033[m")

    if escolha == 3:
        print("Finalizando...")

    elif escolha == 4:

        procurar = input("Digite quem você procura: ")

        print('Usuario cadastrado? {}'.format(procurar in Usuarios))

        def search (usuarios, valor):
            return [(Usuarios.index(x), x.index(valor)) for x in Usuarios if valor in x]

        for i in Usuarios:
            local = Usuarios.index(i)
            for b in i:
                if b == procurar:         
                    print("teste: [{}]".format(local))
                    break

        print("Quem você procura:",lista[local])
        
    elif escolha > 4:
        print(f"\033[1;31mOpção inválida. Tente novamente\033[m")


    elif escolha <= 0:
        print(f"\033[1;31mOpção inválida. Tente novamente\033[m")

    print('=-=' * 10)
print(f"\033[1;32mVocê saiu do programa com exito!! volte sempre!\033[m") ```


1 answer

0


In python there is the operator "in", it returns a boolean value (True or False), if some value exists in a compound variable (List, tuples, even strings).

What you can do is not add the value of the user name directly to your list as you did, but rather store it in a variable and add it to the list only if there is an equal value within the list, and use the index function, that returns the position of a value within a list to display the already registered data.

lista_nome = ['João', 'Matheus', 'Maria']
lista_idade = [18, 35, 76]
nome = str(input('Nome: '))
idade = int(input('Idade: ')

if name in lista:
    print(f'{lista_nome[lista_nome.index(name)]} já está cadastrado e tem {lista_idade[lista_nome.index(name)]} anos')
else:
    lista_nome.append(nome)
    lista_idade.append(idade)

The logic is simple, if the name already exists in the list of names it shows the information, print the name using as position for the list the index function itself to find the name, and similarly in the list of ages to find the corresponding age, and if it does not exist, add it.

you can use "not" too, which in python reverses the value, basically a "no" even, the logic would look like "if name not in lista_name", which would be "if the name is not in the name_list", in this case > you would reverse the conditions, if the name does not exist in the list you the adds

I hope you understand!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.