Mar-29-2017, 04:12 PM
Soooo.... So far this is what I've been able to come up with. I know it's not the most efficient but it does what I need it to for now. I know I will have to end up adding a dictionary for username:password key value pairing and checks.... JSON will probably be my best bet. But as of right now I am proud of my work so here it is. Please comment and critique :D
class Register(): def __init__(self, prompt_message): self.prompt_message = prompt_message def user_registration(self): prompt_username = input(self.prompt_message) filename = 'guest_book.txt' if prompt_username.lower() != 'quit': with open(filename, 'a+') as file_obj: with open(filename, 'r') as file_obj: names = file_obj.readlines() for name in names: if prompt_username.lower() in name.lower(): print("Name Taken") break else: with open(filename, 'a') as file_obj: file_obj.write(prompt_username + '\n') print("Welcome to a whole new world, {}".format(prompt_username)) get_username = Register("Please enter the username you wish to create. \n\tOr type 'quit' to exit: ") get_username.user_registration()I use the first
with open(filename, 'a+') as file_obj:in order to create the file incase it doesn't exist. The readlines to create a list to check for existing usernames and the next 'a' to append usernames that do not exist yet.