Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes and Functions
#11
How would I stop the Jane / Mary Jane situation..?
Reply
#12
Instead of checking for user names check for their hashes. You may store the usernames as pairs: username, hash sum
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
(Mar-30-2017, 11:22 PM)Low_Ki_ Wrote: How would I stop the Jane / Mary Jane situation..?

I showed you. The second bit of code in my last post solves the problem.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
(Mar-31-2017, 01:00 AM)ichabod801 Wrote:
(Mar-30-2017, 11:22 PM)Low_Ki_ Wrote: How would I stop the Jane / Mary Jane situation..?

I showed you. The second bit of code in my last post solves the problem.

Thank you. One thing though. This code:

import os.path

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':
            if not os.path.exists(filename):
                with open(filename, 'w') as file_obj:
                    pass
            with open(filename, 'r') as file_obj:
                names = file_obj.readlines()
            for name in names:
                if prompt_username.lower() == name.strip().lower():
                    print("Name Taken")
                    break
                else:
                    with open(filename, 'a') as file_obj:
                        file_obj.write(prompt_username + '\n')
                        print("Welcome the the world of Kings Men, {}.".format(prompt_username))


get_username = Register("Please enter the username you wish to create. \n\tOr type 'quit' to exit: ")
get_username.user_registration()
Just creates a blank file "guest_book" and doesn't write to it. Gives no traceback.
Reply
#15
I guess that you dont have any names in your guest_names.txt file (eventually you start with no file). You are going through names in your names list and for each name you compare it with username and if they are different, you append username to a file. What will happen if there is no name in your list to start with? And what will happen if there is multiple names in your file?

Perhaps you should change your for loop so its used only to check if username is taken or no.

Creating empty file so you can read empty list from it seems little odd. Isnt easier to just put names = [] if there is no file? (you dont need existing file to append to it).
Reply
#16
(Mar-31-2017, 04:07 PM)zivoni Wrote: I guess that you dont have any names in your guest_names.txt file (eventually you start with no file). You are going through names in your names list and for each name you compare it with username and if they are different, you append username to a file. What will happen if there is no name in your list to start with? And what will happen if there is multiple names in your file?

Perhaps you should change your for loop so its used only to check if username is taken or no.

Creating empty file so you can read empty list from it seems little odd. Isnt easier to just put names = [] if there is no file? (you dont need existing file to append to it).

I will work on that and post the results. Thanks for the tips. Have to go to work soon so the results may be posted tomorrow morning. If anyone else has any other input, I would be grateful.

So I was able to fix it and get the results I've been looking for. Now time to work on passwords I think? And changing the code so that it creates a JSON file with the key:value (username:password in this case) written to it.

import os.path

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':
            if not os.path.exists(filename):
                with open(filename, 'w') as file_obj:
                    file_obj.write(prompt_username + '\n')
            else:
                with open(filename, 'r') as file_obj:
                    names = file_obj.readlines()
                for name in names:
                    if prompt_username.lower() == name.strip().lower():
                        print("Name Taken")
                        break
                    else:
                        with open(filename, 'a') as file_obj:
                            file_obj.write(prompt_username + '\n')
                            print("Welcome the the world of Kings Men, {}.".format(prompt_username))


get_username = Register("Please enter the username you wish to create. \n\tOr type 'quit' to exit: ")
get_username.user_registration()
Reply
#17
Actually how would I take this block and turn it into a while loop so that users can enter a new name if they choose:

else:
                with open(filename, 'r') as file_obj:
                    names = file_obj.readlines()
                for name in names:
                    if prompt_username.lower() == name.strip().lower():
                        print("Name Taken")
                        break
I experimented already and if I put the while loop inside the for loop and it will allow usernames to be entered twice..
Reply
#18
I have never seen username to be entered twice. If the username is not taken already.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#19
(Apr-01-2017, 03:38 AM)wavic Wrote: I have never seen username to be entered twice. If the username is not taken already.

Are you saying I should just leave it as a if statement instead of a while loop? Wouldn't the right while loop check name every time so no same name could be used just in the case someone would try to do that?
Reply
#20
I was thinking that you want the user to enter the username again after hitting the "Enter" button if thoughts.

While True:
    if prompt_username in names:
        print('The user exists!')
        prompt_username = input(prompt_message)
    else:
        break
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,527 Aug-25-2020, 03:43 AM
Last Post: micseydel
  How can classes access each other Functions and Variables at the same time PythonOK 4 3,053 Dec-09-2018, 03:46 AM
Last Post: ichabod801
  Using classes? Can I just use classes to structure code? muteboy 5 5,043 Nov-01-2017, 04:20 PM
Last Post: metulburr
  Python Classes or Functions for large scale application ? Vithulan 5 4,574 Oct-23-2017, 04:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020