Program only works once

Asked

Viewed 40 times

-1

Talk guys, I’m uncrossing python and I’m putting together a simple program that will gradually divide the modules, add treatments etc... The problem is this: in the code below, the program only works once. I create the user and at the end it shows me that the user was created and the key:value and calls the start function, but when I select a new option, the program simply ends. Could someone give me a hint?

    # 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()]()
  • Shouldn’t have a noose to call the function inicio() until you type 4 to get out?

  • Yes, I’m going to implement it, but even without the loop, because I’m following the pathways for it not to end, it should work, right? For example, if I select option 1, then type in a user and password, it displays and calls the start function, because when I choose another option, or even 1 again it ends? Or I’m wrong and that’s just missing the loop?

1 answer

1


What happens is simple within your registration function you call the start function which returns the value of select for nothing. As it 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 the completion of 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 small changes your program should work. But I strongly advise creating a main loop that performs the input and calls the functions. A code this way will be completely problematic when performing future maintenance try to make things clearer.

  • Thanks Joannis, I will test here and study a little more to understand better. Thanks for the advice, I will improve. I’ve even implemented the loop.

  • If you can after testing put as correct I would appreciate it. Avoid these calls known as goto, you may end up creating an unwanted endless looping that will take hours to resolve.

Browser other questions tagged

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