Program only works once

-1

Speak people, I'm debugging python and I'm putting together a simple program that will gradually split the modules, add treatments etc ... The problem is this: in the code below, the program only works once. I create the user and in the end it shows me that the user was created and the key: value and calls the start function, however when I select a new option, the program simply finishes. Could someone give me a tip?

    # cadastar usuario 
    # login
    # sair do sistema
    # mostar tela
    # mostar dados do usuario

    import sys
    bd = {}

    def cadastar_usuario (user = 0, pwd = 0):
      usuario = input("Por favor, digite um nome de usuário: ")
      senha = input("Digite uma senha: ")
      bd.update({usuario: senha})
      print("Usuario cadastrado com sucesso! \n ", bd, "\n\n")
      inicio()

    def entrar():
      print("Usuario entrou")

    def mostrar_dados():
      print("Mostrando dados")

    def sair():
      print("Até a proxima")
      # sys.exit()

    def inicio():
      select = int(input("Escolha uma opcao: \n"
      "1 - Cadastrar usuario \n"
      "2 - Entrar \n"
      "3 - Mostrar dados de usuarios \n"
      "4 - Sair do sistema \n"))
      return select
    opcao = {1:cadastar_usuario, 2:entrar, 3:mostrar_dados, 4:sair}
    opcao[inicio()]()
    
asked by anonymous 29.08.2018 / 12:56

1 answer

1

What happens is simple inside your function register you call the start function that returns the value of select to nothing. As was the so-called option in the last line of your code that triggered these calls it will return from this line, which results in finalizing the program.

# cadastar usuario 
# login
# sair do sistema
# mostar tela
# mostar dados do usuario

import sys
bd = {}

opcao = {1:cadastar_usuario, 2:entrar, 3:mostrar_dados, 4:sair}

def cadastar_usuario (user = 0, pwd = 0):
  usuario = input("Por favor, digite um nome de usuário: ")
  senha = input("Digite uma senha: ")
  bd.update({usuario: senha})
  print("Usuario cadastrado com sucesso! \n ", bd, "\n\n")

  opcao[inicio()]()

def entrar():
  print("Usuario entrou")

def mostrar_dados():
  print("Mostrando dados")

def sair():
  print("Até a proxima")
  # sys.exit()

def inicio():
  select = int(input("Escolha uma opcao: \n"
  "1 - Cadastrar usuario \n"
  "2 - Entrar \n"
  "3 - Mostrar dados de usuarios \n"
  "4 - Sair do sistema \n"))
  return select

opcao[inicio()]()

With these minor changes your program should work. However I strongly advise to create a main loop that performs the input and calls the functions. A code like this will be completely problematic when performing future maintenance try to make things clearer.

    
29.08.2018 / 13:25