Reputation: 27
I have to create a dictionary (in your file) of four-person from my course and the name of the group they belong to. When I run my program it should ask the user to enter a name and return the name of the group of that person back to them. It should look something like this:
Welcome to the py-group-infromator, I can tell you where those users belong to:
{name_user}
{name_user}
{name_user}
{name_user}
Which user do you want to ask for?
{name_user}
{name_group}
in the beginning
notes = ''' "Welcome to the py-group-informator,
I can tell you where those users belong to" :
Azura
Mate
Anna
John
" Which user do you want to ask for ?" '''
print(notes)
My dictionary
people = [{'name': "Azura", 'group': "cute_python"},{'name': "Mate", 'group': "cute_python"},{'name': "Anna", 'group': "fatal_error"},{'name': "John", 'group': "fatal_error"}]
Could any help me? Big sorry for my style, this is my first ask ;)
Upvotes: 0
Views: 73
Reputation: 7887
Similar to the other answer but I would write it a tad different:
people = [
{'name': "Azura", 'group': "cute_python"},
{'name': "Mate", 'group': "cute_python"},
{'name': "Anna", 'group': "fatal_error"},
{'name': "John", 'group': "fatal_error"}
]
name_to_group = {d['name']: d['group'] for d in people}
print("Group Information")
names = ', '.join(name_to_group)
name = input(f"Enter one of {names} or 0 to exit: ")
while name != '0':
if name not in name_to_group:
continue
print(f"{name} is in group {name_to_group[name]}")
name = input(f"Enter one of {names} or 0 to exit: ")
print('Good Bye')
Example Output:
Group Information
Enter one of Azura, Mate, Anna, John or 0 to exit: Mate
Mate is in group cute_python
Enter one of Azura, Mate, Anna, John or 0 to exit: John
John is in group fatal_error
Enter one of Azura, Mate, Anna, John or 0 to exit: 0
Good Bye
Upvotes: 1
Reputation: 5140
Following is the core logic to extract from you data structure (purposefully omitting your list and welcome screen text). Assuming you have those, first capture the user input as below and continue to search for group that they belong to.
user_name = input("Which user do you want to ask for ?")
for item in people:
for key in item:
if item[key] == user_name:
print(item['group'])
break
Upvotes: 0
Reputation: 709
Try this:
people = [
{'name': "Azura", 'group': "cute_python"},
{'name': "Mate", 'group': "cute_python"},
{'name': "Anna", 'group': "fatal_error"},
{'name': "John", 'group': "fatal_error"}
]
def op(names):
for value in people:
if value['name'].lower() in names.lower():
print(value['group'])
x = op(input("Welcome to the py-group-information,I can tell you where
those users belong to : Azura Mate Anna John Which user do you want
to ask for ?"))
Upvotes: 0