How to make sure that the user enters a password that matches the username registered on this password?

I am curious how to create an example of a program in which a user logs in (I know how to do this part), but when a user logs in and the username is added to the list or dictionary that they have a lot of, it cannot be another user password, because if I do "if x to x:" because it will allow another user to use a password to access another user. I just want the correct data to match.

#Example

users = ["bob", "joe"]
passwords = ["example1", "example2"]

ex1 = input("What is your username?")
if ex1 in users:
    ex2 = input("enter your password")
if ex2 in passwords:
    print("access granted")
#With this code any password will work with any user,
#I want the registered user password to have to be the password they registered with
+4
source share
2 answers

, .

users = ["bob", "joe"]
passwords = ["example1", "example2"]

# d = {'bob' : 'example1', 'joe' : 'example2'}
# An example how to zip your lists into a dictionary
d = dict(zip(users, passwords))

ex1 = input("What is your username?")
if ex1 in users:
    ex2 = input("enter your password")

# How to use your dict to check if the password matches the user
if ex2 == d[ex1]:
    print("access granted")
+4

:

users = {"bob": "example1", "joe": "example2"}
ex1 = raw_input("What is your username?: ")
if ex1 in users:
    ex2 = raw_input("enter your password: ")
if ex2 == users.get(ex1):
    print("access granted")
+1

Source: https://habr.com/ru/post/1675912/


All Articles