How can I check if two values of two matrices are in the same position?

Asked

Viewed 44 times

0

I am trying to create a mini login system using python, where a login and password are requested, and if the two match, for example: the login is at position 2 and the password is also at position 2, the login is approved. My problem is in checking if the login and password are in the same position, how can I do that? In my current code, any password gives access to any login. I have tried to put x == y and it didn’t work, and I also researched about, but I only found content on Numpy, can you use this library to solve my problem? (I’m starting to program now, sorry for the bad practices, if you have any other tips, I accept)

username = ['pedro', 'marcos', 'ana', 'michael', 'tomas']
key = ['pedro12', 'markos', 'anaconda', 'jackson', 'toma']

user = str(input("Digite seu username: "))
if user in username:
   for x in username:
    if user == x:
        senha = str(input("Digite sua senha: "))
        for y in key:
            if senha in key:
                if senha == y:
                    print("Olá {}".format(x))
            else:
                print('Acesso negado')
else:
    print("Username não encontrado")

2 answers

2

Alive, you can simplify a lot and you don’t even need to use for loop, but instead of using two arrays which seems strange to validate the position, use instead a Dictionary, with the key is your name and value is the password.

NOTE: Take a look at the documentation, here and here. Note from Fernando. It’s also important to look at the complexity analysis of algorithms, here.

nomes = {'pedro':'pedro12', 'marcos': 'markos', 'ana': 'anaconda', 'michael': 'jackson', 'tomas': 'toma'}

def login():
  user = str(input("Digite seu username: "))
  if user in nomes.keys():
      senha = str(input("Digite sua senha: "))
      if senha in nomes[user]:
          print("Olá {}".format(user))
      else:
          print('Acesso negado')
  else:
      print("Username não encontrado")

Or

Using your data source and get by index if the password exists at that position, again without any loop.


username = ['pedro', 'marcos', 'ana', 'michael', 'tomas']
key = ['pedro12', 'markos', 'anaconda', 'jackson', 'toma']

def login():
  user = str(input("Digite seu username: "))
  if user in username:
      index = username.index(user)
      senha = str(input("Digite sua senha: "))
      if senha in key[index]:
          print("Olá {}".format(user))
      else:
          print('Acesso negado')
  else:
      print("Username não encontrado")
  • 1

    +1 for displaying with dictionary and still display method list.index. I would only advise adding link to the documentation (that one or that one would already help).

0


See if that’s what you want:

username = ['pedro', 'marcos', 'ana', 'michael', 'tomas']
key = ['pedro12', 'markos', 'anaconda', 'jackson', 'toma']

user = str(input("Digite seu username: "))
if user in username:
   for i, x in enumerate(username):
    if user == x:
        print("Usuário na posição "+str(i))
        senha = str(input("Digite sua senha: "))
        if senha in key:
            for j, y in enumerate(key):
                if i == j and senha == y:
                    print("Senha na posição "+str(j))
                    print("Olá {}".format(x))
                    #Quebra o for para não precisar gastar processamento
                    #pesquisando outras senhas.
                    break
                else:
                    print("Senha na posição "+str(j))
                    print('Acesso negado')
                    break
else:
    print("Username não encontrado")

What I did was take the index in the for to be able to compare and your "if password in key:" was after the for, and I put before.

Browser other questions tagged

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