E
E
expp2020-05-07 20:26:49
Python
expp, 2020-05-07 20:26:49

How to display the values ​​of a key in a dictionary if we don't know its name?

Good evening. Let's agree that we have a dictionary that contains Login:Password pairs. If the user (through input, of course) enters a login and this login is in our dictionary, then the password is displayed to him. The difficulty lies in the fact that we do not know what login the user will enter, therefore, neither the name nor the order of this login in the dictionary is known. How can I do that? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-05-07
@expp

users = {
    'expp': 'qwerty',
    'sergey-gornostaev': 'nA7L$45f_uKv'
}

username = input('Введите имя пользователя')
if username in users:
    password = input('Введите пароль')
    if password == users[username]:
        print('Добро пожаловать, ' + username)
    else:
        print('Неверный пароль')

R
Roman Kulshin, 2020-05-07
@Gramor

There are two dictionary methods for this.
1) .items() - Throws us key value pairs.
2) .keys() - returns all keys.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question