1

How do I make this code store new usernames and passwords in a dictionary with username as (K) and password as (V)? Or do I need to store them in a list, if so how would i do it? I've already written another piece of code for logging in of returning users that accesses the username and password as (K) and (V) in the dictionary.

while True:
    new_user = input("Consider using alphanumeric and special characters \nCreate new user ID: ")
    if len(new_user) < 4:
        print("User ID must be more than 4 alphanumeric characters".upper())
        continue
    elif len(new_user) > 10:
        print("User ID must be less than 10 alphanumeric characters".upper()) 
        continue  
    else:
        print("Please confirm user ID ")
        break
F.M
  • 681
  • 9
  • 24
Rich
  • 25
  • 4

3 Answers3

2

You can create a new dictionary with mydict={}and then store keys and their values with mydict["your-key"] = "your-value"

You can find more here, keep in mind that saving textplain passwords is really dangerous, in particular in this way!

In addition, as user8408080 said:

You would normally instantly hash the password and save that hash. When the user tries to log in with his password, that is also going to be hashed and the hashes will be compared. That way nobody can steal your password from a database

Gray
  • 333
  • 2
  • 15
  • 1
    Addition: In case OP is wondering, what else to do: You would normally instantly hash the password and safe that hash. When the user tries to log in with his password, that is also going to be hashed and the hashes will be compared. That way nobody can steal your password from a database – user8408080 Jul 06 '20 at 21:38
  • Good, thanks! Can i add it to the answer and tag u? – Gray Jul 06 '20 at 21:48
  • No problem, that's what comments are for :D – user8408080 Jul 07 '20 at 12:46
2

To build further on the code you've provided, here's a small example. You'll first have to create your dictionary of course. I called it users_and_passwords. Keep in mind that storing users and passwords directly in a dictionary is not at all encouraged!

users_and_passwords = dict()

# get-user loop
while True:

    new_user = input("Consider using alphanumeric and special characters \nCreate new user ID: ")
    if len(new_user) < 4:
        print("User ID must be more than 4 alphanumeric characters".upper())
        continue
    elif len(new_user) > 10:
        print("User ID must be less than 10 alphanumeric characters".upper()) 
        continue  
    else:
        print("Please confirm user ID ")
        break


# get-password loop
while True:
    
    user_password = input("Create new user password: ")

    # do whatever logic here
    # let's assume its fine now
    break

After you got your user and password variables filled properly, you can add them as a key:value pair to the dictionary like this:

users_and_passwords[new_user] = user_password

You can then iterate over your users and their passwords like so:

for k, v in users_and_passwords.items():
    print (f'user: {k}\thas password: {v}')

To show that this works, with user123 as username and pass123 as a password in this example the output will be:

user:user123    has password: pass123

Watch out with storing passwords directly!

As others have pointed out already, simply scrambling your password irreversibly (e.g. by hashing it, you could take a look at this) and using the scrambled pass as a value for your user would make this a bit safer.

Queuebee
  • 430
  • 1
  • 4
  • 13
1
users = [{'K':'john','V':'123456'}]

while True:
  username = input("Consider using alphanumeric and special characters \nCreate new user ID: ")
  if len(username) < 4:
    print("User ID must be more than 4 alphanumeric characters".upper())
    continue
  elif len(username) > 10:
    print("User ID must be less than 10 alphanumeric characters".upper()) 
    continue  
  else:
    # getting the password
    password = input('Password: ')
    # creating a new user with a dict
    new_user = {'K':username,'V':password}
    # adding the new user to dict
    users.append(new_user)

    # I added this for testing purposes, delete these 3 lines in real use
    if(len(users) > 1):
      break
    continue
    
    # and uncomment the next line
    # break

def login(username, password):
  for user in users:
    # checking if the given username and password matches a user in the users list
    if(user['K'] == username and user['V'] == password):
      print('Logged in')
      return True
  
  return False


print('\n\n\n')
print(users)
print('\n\n\n')
print('Log in')
username = input('Username: ')
password = input('Password: ')

login(username,password)

I added the users to a list and in login searched trough the list and checked for username and password matches.

Note: In production never save password in plain text, hash them with something like sha256

  • why would you use a list of dictionaries instead of just a dictionary? That's sort of the worst of both worlds... – juanpa.arrivillaga Jul 06 '20 at 22:01
  • I just created an example here, in these cases a database should be used for efficiency. and I didn't want to store every username in a dict, actually it would be much better if I did liked that, sry – tolgaerdonmez Jul 06 '20 at 22:05