I am making a login/ sign up page in python for fun but im kinda stuck. I have made my sign up page but im stuck with the login page I need some code that reads the text file(info.txt) for what the user inputted and if they are both there in the same line it will print something https://i.stack.imgur.com/ONsWB.png <- here is what my files look like and here is my sign up code
print("Welcome to sign up Enter your details below")
username = input("Enter Username:")
password = input("Enter Password:")
#input text
input_dictionary = {"one" : 1, "two" : 2}
#open file
file = open("info.txt", "w")
#convert variable to string
str = repr(username)
lol = repr(password)
file.write(str ":" lol "\n")
#close file
file.close()
f = open('info.txt', 'r')
if f.mode=='r':
contents= f.read()
print("Well done you now have a account!")
and so far my login page code is
username = input("Username:")
password = input("Password:")
CodePudding user response:
Something like this should work
username = input("Username:")
password = input("Password:")
with open('info.txt') as f:
# split file on newline
users = f.read().split('\n')
if username ":" password in users:
# user logged in
CodePudding user response:
you could do something like this:
import time as t
f_content = open("info.txt",'r').read()
while True:
username = input("Username:")
password = input("Password:")
infos = [username,password]
for user in f_content:
user_info = user.split(":")
if user_info == infos:
found = True
print("Successfully logged in!")
#some code
if not found:
print("User was not found!\nWait 20 seconds to reset")
t.sleep(20)
and you should always put things like this in functions, to avoid calling external modules:
import time as t
f_content = open("info.txt",'r').read()
def signin():
print("Welcome to sign up Enter your details below")
username = input("Enter Username:")
password = input("Enter Password:")
#input text
input_dictionary = {"one" : 1, "two" : 2}
#open file
file = open("info.txt", "w")
#convert variable to string
str = repr(username)
lol = repr(password)
file.write(str ":" lol "\n")
#close file
file.close()
f = open('info.txt', 'r')
if f.mode=='r':
contents= f.read()
print("Well done you now have a account!")
def login(username,password):
infos = [username,password]
for user in f_content:
user_info = user.split(":")
if user_info == infos:
found = True
print("Successfully logged in!")
#some code
if not found:
print("User was not found!\nWait 20 seconds to reset")
t.sleep(20)
if __name__ == '__main__':
if f_content == '':
signin()
exit()
while True:
username = input("Username >> ")
password = input("Password >> ")
login(username,password)
if you though i overcomplicated things, you should do just like this:
username = input("Username:")
password = input("Password:")
f = open('info.txt')
# split file on newline
users = f.read().split('\n')
if username ":" password in users:
# do some code
...
