0

I am writing a Python script that takes a username and determines if it is a valid username. If it is different from any other username and is greater than 6 characters, it is added to a list of usernames. If the username is shorter than 6 characters, or is the same as an existing username, the program ends and informs the user of the invalid name. My issue is if I put in a new username which gets added to the list, and then put in that same username again, it does not inform me that it is already taken.

name_list = []
username = input("input your new username: ")
if len(username) < 6:
    print('username is too short')
elif len(username) >6:
    if username in name_list :
        print('username taken')
    elif username not in name_list:
        name_list.append(username)

Say I input JonnyBoy as the username; it gets added to the name_list. If I then run the program again using the same username, it does not recognize it is the same as an existing username.

Stev
  • 1
  • 1
  • Well where are you saving the usernames? Doesn't look like anywhere – rahlf23 Mar 29 '18 at 20:03
  • When you run the program again, it starts over with a whole new empty namespace, and then you do `name_list = []` and that's your list. If you want to store information across separate runs, you need to explicitly save it to disk somewhere and then load it back at startup. There are many ways to do that—json, csv, pickle, dbm, redis, sqlite3—with different strengths and weaknesses. – abarnert Mar 29 '18 at 20:03
  • every time you run the script it initializes `name_list` as `[]` and you're not saving the names anywhere so....how would it know? – eagle Mar 29 '18 at 20:03
  • [The tutorial](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) is helpful here (but make sure you read all the way down to the JSON part). – abarnert Mar 29 '18 at 20:04
  • I recommend skipping everything that contains the word "pickle" while you're researching data persistence. – Aran-Fey Mar 29 '18 at 20:05
  • @Aran-Fey Wow, the answers there are a bit out-of-date. Skimming them the implication you'd get is that JSON is some flash-in-the-pan thing that may be in the stdlib if you have a bleeding-edge version of Python, but be careful… Things have changed a bit in the intervening decade. – abarnert Mar 29 '18 at 20:06
  • @abarnert I can't find anything better :( And to make things worse, `pickle` is _always_ the top answer :/ – Aran-Fey Mar 29 '18 at 20:12
  • @Aran-Fey Well, at least it doesn't have a big warning to always use cPickle, beware of Python 3, and follow these steps to guarantee that your pickling is (misleadingly-apparently) "safe" like many old answers… And we can upvote the answers for the tutorial chapter, JSON, etc. So, good find, given the options… – abarnert Mar 29 '18 at 20:19

1 Answers1

2

Your list gets reset everytime you restart your script. You need to store the list in a file. Read that file next time you run the script to get the old list. Have a look at this article: http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

In this case I think the json module will do a great job. Just call json.dump(name_list, file) to store the list.

Example for writing the list to a file:

with open("somefile.txt", "w") as file:
    json.dump(name_list, file)

And to read it:

with open("somefile.txt") as file:
    name_list = json.load(file)
Tim Woocker
  • 1,883
  • 1
  • 16
  • 29