[英]how to return a dictionary key as a string value Python
我是一个初学者程序员,我有一个问题。 我需要在字典中将键值作为字符串返回。
choice = skills[(input('which attribute would you like to access: '))]
print('Testing, choice was in word')
user = None
while user != 0:
print('\nyour current', choice[0] ,' skill is:', choice,)
print('you also have', XP,'XP points left to distribute')
print('''\nWould you like to Add or take away points
0. Go back
1. Add
2. Take away''')
就在choice[0]
我希望程序将键值返回给字典。 然后说出'skill is: '
(选择)),然后返回该键的值。
我希望能够仅将键显示为字符串值,以便在程序中轻松访问。
因此,例如,如果键是“ X”且值是“ x”,我希望程序吐出。
'your current X skill is x'
到目前为止,打印功能的末尾工作正常。
假设您有一个字典,例如: d = {'X':'x'}
您可以从字典中将值检索为d['X']
函数keys()
将返回字典中的键列表,而values()
将返回字典中的值列表。 而且,如果您不知道键并想要检索任何项目,则可以在keys()
返回的列表中使用索引i
。 尝试这个:
d = {'X':'x'}
print('\nyour current', d.keys()[0] ,' skill is:', d[d.keys()[0]])
您的程序的预期解决方案可以是:
choice = input('which attribute would you like to access: ')
print('\nyour current', skills[choice] ,' skill is:', choice,)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.