如何创建一个 returns 有问题的值(键)的字典?
How to create a dictionary that returns the value(key) in question?
我必须创建一个字典(在你的文件中),其中包含我课程中的四个人以及他们所属小组的名称。
当我 运行 我的程序时,它应该要求用户输入一个名字,然后 return 那个人的组名返回给他们。它应该看起来像这样:
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}
开头
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)
我的字典
people = [{'name': "Azura", 'group': "cute_python"},{'name': "Mate", 'group': "cute_python"},{'name': "Anna", 'group': "fatal_error"},{'name': "John", 'group': "fatal_error"}]
有什么可以帮助我的吗?非常抱歉我的风格,这是我的第一个问题 ;)
试试这个:
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 ?"))
与其他答案类似,但我会写得有点不同:
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')
示例输出:
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
以下是从您的数据结构中提取的核心逻辑(有意省略您的列表和欢迎屏幕文本)。假设你有这些,首先捕获用户输入如下,然后继续搜索他们所属的组。
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
我必须创建一个字典(在你的文件中),其中包含我课程中的四个人以及他们所属小组的名称。 当我 运行 我的程序时,它应该要求用户输入一个名字,然后 return 那个人的组名返回给他们。它应该看起来像这样:
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}
开头
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)
我的字典
people = [{'name': "Azura", 'group': "cute_python"},{'name': "Mate", 'group': "cute_python"},{'name': "Anna", 'group': "fatal_error"},{'name': "John", 'group': "fatal_error"}]
有什么可以帮助我的吗?非常抱歉我的风格,这是我的第一个问题 ;)
试试这个:
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 ?"))
与其他答案类似,但我会写得有点不同:
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')
示例输出:
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
以下是从您的数据结构中提取的核心逻辑(有意省略您的列表和欢迎屏幕文本)。假设你有这些,首先捕获用户输入如下,然后继续搜索他们所属的组。
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