Infinite registration of client

Asked

Viewed 331 times

0

I’m studying Python and decided to create a small terminal registration program, but I can’t make a loop that goes through all the options, the program is interminably in the client registration.

Follows code:

#cadastro de cliente em programacao procedural

clientes = []
n_clientes = 1

def menu() :
    option = int(input('''
[1] - Cadastrar cliente
[2] - Consultar Clientes
[3] - Editar Cliente
[4] - Sair do programa
'''))

    return option

def cadastra_cliente() :
    cliente_nome = input('Digite o nome do cliente: ')
    cliente_cep = input('Digite o cep do cliente: ')
    cliente_telefone = input('Digite o telefone do cliente: ')
    clientes_dados = (cliente_nome,cliente_cep,cliente_telefone)
    clientes.append(clientes_dados)
    print(clientes)
    print('Cliente adicionado')

def mostrar_cliente() :
    print(f'''
    Nome: {clientes[0]}
    Cep: {clientes[1]}
    Telefone: {Clientes[2]}''')


def programa() :

    option = menu()
    while True:
        if option == 1 :
            cadastra_cliente()
        if option == 2 :
            mostrar_cliente()
programa()

Could you help me? I managed to do this in Java once with do {} while, but I can’t do it in Python.


One more question only!

IN java when I made a similar program, to change the registration I had the following structure.

public static void editarCadastro() {
    System.out.print("Informe o codigo que gostaria de atualizar: \n");
    int posicaoPessoa = Entrada.leiaInt();
    String pessoa[] = listaDePessoas.get(posicaoPessoa);

    if (pessoa != null) {
        System.out.println("Nome: '" + pessoa[0] + "'");
        pessoa[0] = Entrada.leiaString();
        System.out.println("CEP: '" + pessoa[1] + "'");
        pessoa[1] = Entrada.leiaString();
        System.out.println("Endereço : '" + pessoa[2] + "'");
        pessoa[2] = Entrada.leiaString();
        System.out.println("E-mail: '" + pessoa[3] + "'");
        pessoa[3] = Entrada.leiaString();
        System.out.println("Telefone: '" + pessoa[4] + "'");
        pessoa[4] = Entrada.leiaString();

        System.out.println("Pessoa atualizada");
    } else {
        System.out.println("Pessoa não encontrada");
    }
}

I walk with enough difficulty to work with arrays in python, I would have some tips on how I could do something like only in python?

1 answer

2


This is because after the first time the user chooses the menu option, the variable option no longer has its value changed, and you are inside a while True.

A simple fix would be to place the snippet that prompts the menu option to the user within the while True:

while True:
    option = menu()

With this, the program would question the user again, allowing it to choose another option.

So to exit the program, you must implement a condition for option 4, allowing you to exit the loop, something like this:

while True:
    option = menu()

    if option == 1 :
        cadastra_cliente()
    if option == 2 :
        mostrar_cliente()
    if option == 4 :
      break

Note that now, in addition to always asking the user which option he wants, there is a condition that makes the break to exit the loop and finish the program.


The complete code would look more or less like this:

#cadastro de cliente em programacao procedural

clientes = []
n_clientes = 1

def menu() :
    option = int(input('''
[1] - Cadastrar cliente
[2] - Consultar Clientes
[3] - Editar Cliente
[4] - Sair do programa
'''))

    return option

def cadastra_cliente() :
    cliente_nome = input('Digite o nome do cliente: ')
    cliente_cep = input('Digite o cep do cliente: ')
    cliente_telefone = input('Digite o telefone do cliente: ')
    clientes_dados = (cliente_nome,cliente_cep,cliente_telefone)
    clientes.append(clientes_dados)
    print(clientes)
    print('Cliente adicionado')

def mostrar_cliente() :
    for cliente in clientes:
      print(f'''
      Nome: {cliente[0]}
      Cep: {cliente[1]}
      Telefone: {cliente[2]}''')


def programa() :

    while True:
        option = menu()

        if option == 1 :
            cadastra_cliente()
        if option == 2 :
            mostrar_cliente()
        if option == 4 :
          break

programa()

Note: I made a small change in function mostrar_cliente, it was generating exception, I created a simple loop, to iterate in the variable clientes and thus display all customers on the console.

See online: https://repl.it/repls/PlainGoldenrodProgram

Browser other questions tagged

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