Posted January 10, 2019 So I am rewriting a bit of my code, but I have hit a roadblock. I am trying to have a while loop that will test for two regular expressions. My code is: (i added a bit of pseudocode btw) def authentication(): print("*------------------------------------------------------------*") file = open("users.txt") text = file.read() user1NameInput = input("What is your name, user 1?: ") user2NameInput = input("What is your name, user 2?: ") re_res1 = re.search(r'^{}, (.*)$'.format(user1NameInput), text, re.MULTILINE) re_res2 = re.search(r'^{}, (.*)$'.format(user2NameInput), text, re.MULTILINE) while re_res1 and re_res2: ask for password and play game else: add usernames to the file Specs: Motherboard: Gigabyte Z97X Gaming 3 CPU: Intel Core I7 4790K GPU: Gigabyte G1 Gaming GTX 970 RAM: HyperX Fury 16GB HDD: Seagate ST3000DM001 3TB SSD: KINGSTON SV300S37A480G 450GB Cooler: Corsair H100i GTX Case: NZXT H440 Red Monitor: DELL U2412M Keyboard: Gigabyte Force K7 Mouse: Corsair Sabre RGB Link to comment https://linustechtips.com/topic/1019695-python-while-regular-expressions-help/ Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted January 10, 2019 I'd highly recommend creating a user class. This should get you started: #!/usr/bin/env python3 import re class User: def __init__(self, name): self.name = name def auth(*users): with open("users.txt") as file: text = file.read() for user in users: res = re.search(r'^{}, (.*)$'.format(user.name), text, re.MULTILINE) while res: # Login else: # Register if __name__ == '__main__': user1 = User('username1') user2 = User('username2') auth(user1, user2) Link to comment https://linustechtips.com/topic/1019695-python-while-regular-expressions-help/#findComment-12176620 Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted January 10, 2019 (edited) 1 hour ago, DerpDiamonds1 said: So I am rewriting a bit of my code, but I have hit a roadblock. I am trying to have a while loop that will test for two regular expressions. My code is: (i added a bit of pseudocode btw) def authentication(): print("*------------------------------------------------------------*") file = open("users.txt") text = file.read() user1NameInput = input("What is your name, user 1?: ") user2NameInput = input("What is your name, user 2?: ") re_res1 = re.search(r'^{}, (.*)$'.format(user1NameInput), text, re.MULTILINE) re_res2 = re.search(r'^{}, (.*)$'.format(user2NameInput), text, re.MULTILINE) while re_res1 and re_res2: ask for password and play game else: add usernames to the file You can't have a while loop followed by an else statement. else is for the default case if an if-statement doesn't run. You should be using an if-statement for checking to see if the regular expressions found something rather than using a while loop. If the point of the authentication function is to, well, authenticate, it doesn't make sense for it to handle the main loop to run the game. EDIT: Never mind, I found Python allows for elses after whiles (which is weird, but okay) Edited January 10, 2019 by Mira Yurizaki I have a blog! And a list of guides I've posted Link to comment https://linustechtips.com/topic/1019695-python-while-regular-expressions-help/#findComment-12176750 Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted January 10, 2019 Author So is there another way to authenticate usernames and passwords without using regular expressions. Mabey with an array? Specs: Motherboard: Gigabyte Z97X Gaming 3 CPU: Intel Core I7 4790K GPU: Gigabyte G1 Gaming GTX 970 RAM: HyperX Fury 16GB HDD: Seagate ST3000DM001 3TB SSD: KINGSTON SV300S37A480G 450GB Cooler: Corsair H100i GTX Case: NZXT H440 Red Monitor: DELL U2412M Keyboard: Gigabyte Force K7 Mouse: Corsair Sabre RGB Link to comment https://linustechtips.com/topic/1019695-python-while-regular-expressions-help/#findComment-12177450 Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted January 11, 2019 It might be easier to authenticate/create one user at a time, something like: def authenticate(): username = input("What is your username?: ") with open('users.txt') as f: text = f.read() res = re.search(r'^{}, (.*)$'.format(username), text, re.MULTILINE) if res: password = input("What is the password for {}?: ".format(username)) # check password else: # create user return username def run(): print('User 1, Please log in.') user1 = authenticate() print('User 2, Please log in.') user2 = authenticate() # play game Link to comment https://linustechtips.com/topic/1019695-python-while-regular-expressions-help/#findComment-12179522 Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted January 11, 2019 22 hours ago, DerpDiamonds1 said: So is there another way to authenticate usernames and passwords without using regular expressions. Mabey with an array? Store the usernames and passwords in a structured format, like either as a JSON or a CSV. You can then break up the file into either objects (if you use JSON) or as a multi-dimensional list (CSV). Would recommend the JSON path because usernames are supposed to be unique, and so it'd be much easier to access the data blob you neeed. Then you could just do stored_password == inputted_password I'm guessing you're doing this for an exercise, but if you are thinking about doing serious deployment of this application, I strongly recommend you don't store plaintext passwords. At the very least, you should use a hashing algorithm like SHA1 or SHA2. I have a blog! And a list of guides I've posted Link to comment https://linustechtips.com/topic/1019695-python-while-regular-expressions-help/#findComment-12180794 Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted January 12, 2019 23 hours ago, Meic said: It might be easier to authenticate/create one user at a time, something like: def authenticate(): username = input("What is your username?: ") with open('users.txt') as f: text = f.read() res = re.search(r'^{}, (.*)$'.format(username), text, re.MULTILINE) if res: password = input("What is the password for {}?: ".format(username)) # check password else: # create user return username def run(): print('User 1, Please log in.') user1 = authenticate() print('User 2, Please log in.') user2 = authenticate() # play game How can i then assign each username to user1 and user2? Link to comment https://linustechtips.com/topic/1019695-python-while-regular-expressions-help/#findComment-12183078 Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Posted January 12, 2019 1 hour ago, Oliver123456 said: How can i then assign each username to user1 and user2? Use a class. Link to comment https://linustechtips.com/topic/1019695-python-while-regular-expressions-help/#findComment-12183181 Share on other sites More sharing options... Link to post Share on other sites More sharing options...
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now