Reputation: 55
I am trying to make a logging in system with python. To login you must first register. After you have registered it stores it into a text file with the password in base64. Then if you login it asks for username and password which the password gets encoded into base64 and is checked against the text document. But when it searches for the password nothing happens. It just ends the program and goes to the shell. No traceback error or anything. It is as though the rest of the code is invisible. Code is below:
try:
import linecache
import base64
import tkinter
import time
choice = ""
def choose():
choice = input("Would you liek to register or login: ")
choice = choice.upper()
if choice == "REGISTER":
f = open("h.txt", "a")
f.write("\n")
print("Please enetr a username")
username = input(">>> ")
print("Please enter a password")
passw = input(">>> ")
print("\n"*100)
passw_conf = input("Please enter your password again\n>>> ")
print("\n"*100)
if passw == passw_conf:
print("Thank you for Registering")
f.write(username + "\n")
passw = base64.b64encode(passw.encode('utf-8'))
passw = str(passw)[2:-1]
f.write(passw + "\n")
f.close()
choose()
else:
print("Please choose register again as passwords do not match.")
elif choice == "LOGIN":
username = input("Please enter your username\n>>> ")
passw = input("Please enter your password\n>>> ")
x = 0
with open("h.txt") as f:
lines = [line.rstrip('\n') for line in open('h.txt')]
while lines[x] != username:
print(lines[x])
if lines == username:
print("Got it ;)")
elif lines != username:
print("Not got it ;(")
passw = base64.b64encode(passw.encode('utf-8'))
passw = str(passw)[2:-1]
if passw == lines:
print("Logged in successfully")
elif lines == "":
print("Username not found!")
choose()
except KeyboardInterrupt:
print("Restarting....")
time.sleep(8)
print("\n"*100)
choose()
choose()
Upvotes: 0
Views: 141
Reputation: 618
Executed once, we expect the first line should be username, this means, the while
breaks at x=0
(while-start).
Then the variable lines
is a list, it does not equals to the password and it is not empty (no prints). Sorry the program does what you wrote!
Other things that I see:
if lines[x] == username
cannot work for lines[x] != username
)There is a simple way to debug this: print!
lines
)Upvotes: 1
Reputation: 42748
There are many problems with your code. It works only, if the first username in h.txt
is the entered one, otherwise you have a infinite loop. Second, lines
is a list, so never equal to passwd
or ""
. Therefore nothing is printed.
It should look like this:
username = input("Please enter your username\n>>> ")
passw = input("Please enter your password\n>>> ")
with open("h.txt") as f:
lines = [line.rstrip('\n') for line in f]
for line in lines:
if line == username:
user_password = next(lines)
break
else:
print("Username not found!")
return
passw = base64.b64encode(passw.encode('utf-8'))
passw = passw[2:-1]
if passw == user_password:
print("Logged in successfully")
else:
print("Wrong password")
Upvotes: 5