Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes and Functions
#10
You open the same file three times, at the same time, all with the same name. Certainly using the same name at the same time is a bad idea, and having them open at the same time makes me nervous. Since you don't need to do it all at once, I would avoid it.
# check to see if the file exists, and create it if necessary
if not os.path.exists(filename):
    with open(filename, 'w') as file_obj:
        pass
# read the names in
with open(filename, 'r') as file_obj:
    name = file_obj.readlines()
for name in names:
    if prompt_username.lower() in name.lower():
        print('Name taken.')
        break
    else:
        # write to the file
        with open(filename, 'a') as file_obj:
            file_obj.write(prompt_username + '\n')
        print('Welcome to a whole new world, {}.'.format(prompt_username))
I took the final print out of the with clause because it doesn't need to be there. I figure, when you're done with the file, be done with the file.

Note that if you use the in operator to check for existing names, and you have the name 'Mary Jane' in the list, you can't create a new user named 'Jane' (because 'Jane' in 'Mary Jane' is True). I'm not sure if that's what you want. If you are trying to avoid the '\n' at the end of the line, I would use:
if prompt_username.lower() == name.strip().lower():
The strip method will return a string with leading and trailing whitespace (including '\n') removed. The returned string goes in the place of name.strip(), and then can then use the lower method.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Classes and Functions - by Low_Ki_ - Mar-28-2017, 05:43 PM
RE: Classes and Functions - by ichabod801 - Mar-28-2017, 10:31 PM
RE: Classes and Functions - by Low_Ki_ - Mar-29-2017, 03:37 AM
RE: Classes and Functions - by nilamo - Mar-29-2017, 04:10 PM
RE: Classes and Functions - by wavic - Mar-29-2017, 04:00 AM
RE: Classes and Functions - by Larz60+ - Mar-29-2017, 04:48 AM
RE: Classes and Functions - by Low_Ki_ - Mar-29-2017, 03:02 PM
RE: Classes and Functions - by wavic - Mar-29-2017, 03:25 PM
RE: Classes and Functions - by Low_Ki_ - Mar-29-2017, 04:12 PM
RE: Classes and Functions - by ichabod801 - Mar-29-2017, 09:50 PM
RE: Classes and Functions - by Low_Ki_ - Mar-30-2017, 11:22 PM
RE: Classes and Functions - by ichabod801 - Mar-31-2017, 01:00 AM
RE: Classes and Functions - by Low_Ki_ - Mar-31-2017, 03:16 PM
RE: Classes and Functions - by wavic - Mar-30-2017, 11:58 PM
RE: Classes and Functions - by zivoni - Mar-31-2017, 04:07 PM
RE: Classes and Functions - by Low_Ki_ - Mar-31-2017, 04:39 PM
RE: Classes and Functions - by Low_Ki_ - Mar-31-2017, 07:36 PM
RE: Classes and Functions - by wavic - Apr-01-2017, 03:38 AM
RE: Classes and Functions - by Low_Ki_ - Apr-01-2017, 04:48 AM
RE: Classes and Functions - by wavic - Apr-01-2017, 05:15 AM
RE: Classes and Functions - by Low_Ki_ - Apr-01-2017, 03:58 PM
RE: Classes and Functions - by wavic - Apr-01-2017, 08:56 PM
RE: Classes and Functions - by Low_Ki_ - Apr-01-2017, 09:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,686 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,197 Dec-09-2018, 03:46 AM
Last Post: ichabod801
  Using classes? Can I just use classes to structure code? muteboy 5 5,277 Nov-01-2017, 04:20 PM
Last Post: metulburr
  Python Classes or Functions for large scale application ? Vithulan 5 4,718 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