[英]I'm trying to make a dictionary whose name is defined by user input. How do I do this?
I ran into a problem when I tried to do something like this:当我尝试做这样的事情时遇到了一个问题:
a=input()
b=input()
c=input()
a's user input = {'y': b, 'z': c}
Use a dictionary to store your dictionaries.使用字典来存储您的字典。 That allows you to use the input as dictionary key:
这允许您将输入用作字典键:
mydicts = {}
a=input()
b=input()
c=input()
mydicts[a] = {'y': b, 'z': c}
Then later you can retrieve in the same way using mydicts[]
.然后,您可以使用
mydicts[]
以相同的方式检索。 This will print the value of b
when some_var
matches a
:这将打印的值
b
时some_var
匹配a
:
print(mydicts[some_var]['y'])
I guess it should be sth like this:我想它应该是这样的:
d = {}
u_key = ''
while (u_key != 'END'):
u_key = input('Enter key:\n')
d[u_key] = input(f'Enter value for {u_key}\n')
print(d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.