0

Well, I'm developing an application (in kivy) in which user validation is required, so I made a function that will read the .txt file to find the user, if the user is in the file it will be possible to login, if not, not it will be possible. The script in python looks like this:

def login(self):
    user = self.root.ids.user.text
    password = self.root.ids.password.text
    arq = open('arquivo.txt','r')
    count = 1
    while count == 1:
        for line in arq:
            line = line.rstrip()
            if user in line and password in line:
                print('You are CONNECTED')
                count += 1
            if user not in line and password not in line:
                print('Login Error!')
                count -= 1
    arq.close()

The login is working normally, however, when I type a nonexistent user or the incorrect password, the application dies and locks my terminal. Do you wonder why?

terminal print

In the first two lines it is written "Você está LOGADO" which is the same as 'You are connected' but in Portuguese. On the third line, which would be the wrong user simulation, the application died.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • I used 'in' because in the user's registry, I put the 'user', 'password'. Is there a problem in this case? – Enzo Pirinelli Dec 30 '18 at 20:00
  • I did this procedure though, it is giving incorrect user now. The script looks like this: – Enzo Pirinelli Dec 30 '18 at 20:40
  • `def login(self): user = self.root.ids.user.text password = self.root.ids.password.text arq = open('arquivo.txt','r') count = 0 while count == 0: for line in arq: u, p = line.split(",") if user == u and password == p: print('Login!') count += 1 if user != u or password != p: print('Error') count -= 1 print(u) print(p) arq.close()` – Enzo Pirinelli Dec 30 '18 at 20:43
  • I have two lines of text in the .txt file: user1, password user2, password When I try to log in with user1, it appears that it was not possible because it did not find the user and when I ask to print the variables 'u' and 'p', it shows user2 and his password. I thought I'd use the 'count' variable to not use 'break'. – Enzo Pirinelli Dec 30 '18 at 20:56
  • I had not thought of that ... – Enzo Pirinelli Dec 30 '18 at 21:10
  • In the git code, you used the '.split ()' method, but when I use it it appears that 'object has no atributte' split '' – Enzo Pirinelli Dec 30 '18 at 21:13
  • Oh, I'm sorry, I did not see the lack of relatives too ... – Enzo Pirinelli Dec 30 '18 at 21:18
  • Thank you for your help and attention. I'm a beginner with python language, make sure it helped me a lot! – Enzo Pirinelli Dec 30 '18 at 21:23

1 Answers1

0

I think you are trying existing password or user name when trying to login with non-existing user. So when one of them exist in line it dies.

Try like this:

if user not in line or password not in line:

John Doe
  • 13
  • 3
  • Thank you, this part is working now! However, a login problem has arisen: in this case, my admin would be 'enzo_er', but it is not necessary to put the complete user to access, just by typing 'enzo' the login becomes possible. Is there any way to solve this? – Enzo Pirinelli Dec 30 '18 at 19:54
  • Thank you too for your help and attention. I'm a beginner with python language, make sure it helped me a lot! – Enzo Pirinelli Dec 30 '18 at 21:23
  • You can create your text file with key:value style and check the credentials with equality (not using “in”) so it will be ok. – John Doe Dec 31 '18 at 09:32