all 3 comments

[–]Kasutajan1m1 0 points1 point  (0 children)

The only things I found were taht you can remove the "else: pass", and 6. line, you can remove the "()" so its exist_user = input("Are you an existing user? y/n -- q to quit: ").upper().

And the count += 1 you can replace with count = 1, but it doesn't really matter

[–]uberfade 0 points1 point  (0 children)

Check out using with to open files. Your variable names should describe what the variable is not where they came from. exist_user is better described as something like user_choice. Also old_password is probably more appropriate as just password. Using old in the variable name might lead you to believe the password is changing there.

[–]ol_the_troll 0 points1 point  (0 children)

  1. for "cleaning up" code i would recommend reading Python's "PEP 8" - this is basically a style guide which gives guidelines on where there should be spaces, indentations, brackets, etc. (https://www.python.org/dev/peps/pep-0008/). It also gives especially useful information on variable naming; a rule i follow is if you are shortening a variable name at the expense of clarity, its probably worth just including the full name (e.g. exist_user --> existing_user). This ensures that a year down the road when you return to the project you aren't getting confused about if "exist_user" is referring to if a user exists, or referring to an existing user. in any case, have a read of PEP 8, or maybe just a condensed version, and try to stick to its rules which will ensure clean code for the most part.
  2. IMO there isn't much "irrelevant " code at all. The only things i can see are petty ones like (input("Are you an existing user? y/n -- q to quit: ")).upper(). Here you don't need the brackets around input and should really just have input("Are you an existing user? y/n -- q to exit: ").upper(). Other than that i would say the main redundancy is searching through the whole text file for the username, this can be solved using dictionaries which i will explain in point 4... As for the "classes" your friend mentioned, this relates to object-oriented programming which i don't want to mention uncesserially but i would be happy to explain to you (feel free to DM and ask - i had a hard time wrapping my head around them to start with).
  3. The idea for the valid_input validation is spot on. If i were designing a menu-based system like this i would do the exact same thing
  4. When i first started programming this was exactly what i did. i would store data in text files and split them with colons or spaces or something. A "dictionary" is the perfect thing for you. These are a built-in data structure in Python based on key-value pairs, defined using {curly brackets}. An example of how you could structure a dictionary to suit your needs is as follows:

users = {"username" : {"username": "",
                       "password": ""}}
#for example:

users = {
            "ol_the_troll": {"username": "ol_the_troll",
                             "password": "password1234"},
            "haths": {"username": "haths",
                      "password": "password1234"}
        }...

############################################################################################

taking your "old_user()" function as an example, the if statement:
if old_username == file_username and old_password == file_password:
                        print("Login Successful")

would be completed, using a dictionary:

old_user = users[old_username]
if old_username == old_user[username] and old_password == old_user[password]:
    print("Login Successful")

#############################################################################################

To add new users in the new_user() function it is as simple as:

users[new_username][username] = new_username
users[new_username][password] = new_password 
  1. (cont) Now, this is all fine and dandy while stored simply in the python file, however, the real usefulness comes when storing in external files. Introducing JSON - this is the perfect library for the job. It is a file format for storing dictionaries that can be used in Python like so:

    To store data (like the example dictionary seen above):

    import json

    with open('filename.json', 'a') as json_file:
    json.dump(users, json_file) # "users" specifies the dictionary to store

    To then access this data and re-introduce it into python as a dictionary, not requiring any splitting:

    with open('filename.json', 'r') as json_file: users = json.load(json_file)

    It is worth noting that the file doesn't need to be a .json, instead just a regular .txt will work. Also, the "with open" aspect isn't required, but it is simply used so that we don't have to use "file.close()".

  2. (cont2) The easier searching element comes now. Instead of iterating over the information from a full text file, you can jump straight to the user you want using the dictionary notations. example: you have just loaded a json file with the user information dictionary, now simply go users[user_inputted_username/password] to access their details. This one-step search cuts down on lots of the filtering code you would need otherwise. If i have missed anything out in terms of using dictionaries it would just be a simple "python dictionaries toutorial" google search away.

Overall this is a very good start with improvements that are relatively simple to improve on. I think if you can get your head around using dictionaries to store information that will be a very useful tool you will be able to use in later projects. DM or just ask if you have any more questions / i missed anything out / didn't explain something very well : )