Accessing element from tupples

Hi this is RM MUSIC.

I am having a doubt regarding how to access and validate elements from tuples, which are placed inside a list.

Actually, I am working on a small username and password validation project so I decided to use this part of python in my project.

So my code is like this:

L = [(1, 2), (2, 3), (3,4)]

I have thought as the left side of the tuple acting as a username and right side to be for password.

Suppose an user type in my input function like:

Enter username: 1

He types the number 1.
My problem is that how can I access the element from my list and also to print whether the input was write or wrong.

One more problem is that how can I append a tuple in a list which basically contains username and password.

for eg. Username = adorabletrooper
password = adorable1234

So I want it in (“adorabletrooper”, “adorable1234”) form.
So how can I append it in my previous list

And if you have any better method to make this code clean like storing the data in an excel file or in txt file, please write me down.

Please solve my problem as soon as possible.

I think you would be better served by using a dictionary to store your data.

Though, in a real product you would never store plaintext passwords like this.

1 Like

can you tell me, as in the case of using dictionaries that how can I access those elements from the dictionary. I require individual element for individual criterion.
for eg.
list = {“user1”: “user1234”, “user2”: “user2468”}

So if I typed
Enter Your username: user1
Enter Your Password: user1234

The above data is my input to the system, so how can I validate it using dictionaries or better if I say that what is the code to execute such type of data?

You can check if the user is in the dictionary and if the password associated matches.

# Sample user dictionary
myUserDict = {
  "user1" : "user1234",
  "user2" : "user12345"

# Test, user doesn't exist
user = "user3"
print(user in myUserDict)

# Test, user does exist
#   but password is wrong
user = "user1"
pswd = "user1243"
print(user in myUserDict)
print(myUserDict[user] == pswd)

# Test, user does exist
#   and password is correct
user = "user2"
pswd = "user12345"
print(user in myUserDict)
print(myUserDict[user] == pswd)
1 Like

This information is great. I found it quiet efficient.

I had one more difficulty about how can I add data in the same form as in the dictionary?

1 Like

Do you mean adding new entries, like this?

newUser = "user3"
newPass = "user123456"
myUserDict[newUser] = newPass
1 Like

I want the format to be the same as it was used in the dictionary before.

Right, so that snippet above adds new entries to the dictionary that can be accessed in the same way as my larger example.

1 Like

Thanks for being with me to solve my problem. It was nice talking to you.
Have a nice day.

1 Like

How can I write this piece of code in while loop and in if statements.

I don’t know the overall design of your code, so I’m not sure. Though, for any if statements, I imagine that these tests will be useful.

# Check for user in dict
user in myUserDict

# Check for password match
myUserDict[user] == pswd
1 Like

I got the idea. Thanks JeremyLT

1 Like