How would I get the user to register a name and password to log in?

Asked

Viewed 120 times

0

In this code, as I would for the user to register a name and any password to log in?

#Python 3.8.5
inicial = input("Olá!")
inicial2 = input("Você é um novo usuário?")

answer = input("Responda 'Sim' ou 'Não': ") 
if answer == "Sim" or "sim":
    print("Vamos nos cadatrar!")

    def registrar_usuario():
          cadastro_usuario = input("\n\nUsuário: ")
    def registrar_senha():
          cadastro_senha = input("\n\nSenha: ")
          print("Cadatro realizado com sucesso!")
          print("Entre com seu usuário e senha!")
usuario = input("\n\nUsuário: ") 
senha = input("Senha: ") 

#Nessa parte quero que o programa use o nome de usuario e a senha fornecida pelo usuario.
if usuario == ' '   and senha == ' ':
    print("Login realizado")

2 answers

2

You will need to create a (non-volatile) persistence engine, for example, a database (Mysql, Postgree, Oracle, etc.) or a simple local text file (*.txt), where methods registrar_usuário and registrar_senha will save in this mechanism.

Later, you’ll need to change this section below to search your persistence engine and check if the data "matches".

if usuario == USUARIO_SALVO and senha == SENHA_SALVA:
    print("Login realizado")
  • Thanks for your help!

-1


I changed a few things and adapted to something functional. dados_login_e_senhais a dictionary that serves to associate a value to another: A password to a login in this case.

def registrar_usuario(dados):
      cadastro_usuario = input("\n\nUsuário: ")
      if cadastro_usuario in dados:
          print("ERRO! Ja existe um usuario com esse nome")
          return
      cadastro_senha = input("\n\nSenha: ")
      print("Cadatro realizado com sucesso!")
      dados[cadastro_usuario] = cadastro_senha

dados_login_e_senha = {}
answer = input("Você é um novo usuário? Responda 'Sim' ou 'Não': ")

if answer == "Sim" or answer == "sim": 
    print("Vamos nos cadatrar!")
    registrar_usuario(dados_login_e_senha)

usuario = input("Entre com seu usuario: ")
senha = input("Informe a senha: ")
if usuario in dados_login_e_senha:
    if dados_login_e_senha[usuario] == senha:
        print("Login efetuado")
    else:
        print("Senha incorreta")
else:
    print("Nao existe nenhum cadastro com esse nome de usuario")
  • Thanks for the help! Only one "if" was missing in the "IF cadastro_usuario in dados:". But I’m managing to develop my program

  • ops, was really rsrs! I’ll edit

  • 2

    It would be good to make clear that with this method all users and passwords will disappear when the program is terminated.

  • @bfavaretto At some point did I say that the data would persist? In case you hadn’t noticed, she’s starting programming now and her question was about como cadastrar?, something related only to the logic of how to do. At no time did she ask: como faço para que este usuário e senha sirvam para futuras inicializações? I gave the example of a function that did what she wanted. She asked nothing about database or files. ;)

Browser other questions tagged

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