Reputation: 118
Is there a way where a user inputs 2 values like 'username' and 'password'. Can I then check the array to see if these elements are present and then grant the user access? If they are granted access it would print out there admin rights: print('admin')
or print('user')
Array:
myArray = ['bob', '123', 'admin',
'sam', 'qwerty', 'user']
Thanks in advance!
Upvotes: 0
Views: 2353
Reputation: 171
All you really need to do is check if the user is in your user list and make sure the password matches the username. To do that, dictionaries would be best. This way, you can match the password with the username.
users = {'bob': 'pass', '123': 'pass', 'admin': 'pass',
'sam': 'pass', 'qwerty': 'pass', 'user': 'pass'}
Here you set the users and their passwords into a dictionary, you can preset this or you can have a function to create a new user which will be added to the dictionary. A dictionary basically just associates two key values to each other. Ex: (one: 1, two: 2)
login = input("Enter username: ")
password = input("Enter password: ")
Here you're simply setting the variables login and password to user input.
if login in users and users[login] == password:
print("Login successful!")
else:
print("Login invalid")
Finally, here you're checking to see if what the user inputted is in the dictionary of users and passwords. If whatever the user inputs, whether it be username or password, isn't in the dictionary then they will get the login invalid.
I hope this helped you. There are ways to allow the user to create a new profile, if you want to know how to do that just let me know! If you get stuck or don't understand anything I just said, I will of course verify and answer any questions.
Upvotes: 1