2013-12-23 123 views
-2
Pickaxes = { 
'adamant pickaxe': {'cost': 100, 'speed': 5}, 
'bronze pickaxe': {'cost': 100, 'speed': 5}, 
'dragon pickaxe': {'cost': 100, 'speed': 5}, 
'inferno adze': {'cost': 100, 'speed': 5}, 
'iron pickaxe': {'cost': 100, 'speed': 5}, 
'mithril pickaxe': {'cost': 100, 'speed': 5}, 
'rune pickaxe': {'cost': 100, 'speed': 5}, 
'steel pickaxe': {'cost': 100, 'speed': 5}} 

这是我为游戏创建的字典,进入商店时,镐头将打印在屏幕上以显示商店的内容。有没有什么办法可以让他们从字典中显示出斧头的名字和PRICE字样,并在末尾加上这个词。印刷字典内容

ps。请记住,用户将通过一个raw_input来选择他想要的东西,这个raw_input会检查上面的字典,比如用户想要一个符文镐,他们会输入它,它会检查它是否在字典中,我不能添加价格在斧头本身的名义之内。 坚定镐 - 100个金币

+0

你想一个函数如何格式化字典条目,还是要我们写的整个商店为你?你试过什么了? –

+0

我曾尝试将价格添加到词典中的斧头名称,如下所示:Pickaxes {'adamant pickaxe - 100 coins'{cost:100}}但这会更改字典中镐的名称。 – Ali

+0

我想打印这样的东西,而不必添加到镐的名称,所以可能是这样的:print pickaxes ['cost']但这不起作用。 – Ali

回答

0

我想你想知道如何打印的关于斧头的信息,也许下面的代码可以帮助你了解做什么,但没有你已经尝试过什么更多的代码,这将是很难给你工作解决你自己的问题:

>>> Pickaxes = { 
'adamant pickaxe': {'cost': 100, 'speed': 5}, 
'bronze pickaxe': {'cost': 100, 'speed': 5}, 
'dragon pickaxe': {'cost': 100, 'speed': 5}, 
'inferno adze': {'cost': 100, 'speed': 5}, 
'iron pickaxe': {'cost': 100, 'speed': 5}, 
'mithril pickaxe': {'cost': 100, 'speed': 5}, 
'rune pickaxe': {'cost': 100, 'speed': 5}, 
'steel pickaxe': {'cost': 100, 'speed': 5}} 

>>> for axe, values in Pickaxes.items(): 
    print '{} - costs: {}, speed: {}'.format(axe, values['cost'], values['speed']) 

bronze pickaxe - costs: 100, speed: 5 
mithril pickaxe - costs: 100, speed: 5 
adamant pickaxe - costs: 100, speed: 5 
steel pickaxe - costs: 100, speed: 5 
iron pickaxe - costs: 100, speed: 5 
rune pickaxe - costs: 100, speed: 5 
dragon pickaxe - costs: 100, speed: 5 
inferno adze - costs: 100, speed: 5 

你可以简化这样这个过程:

>>> Pickaxes = { 
'adamant pickaxe': {'name': 'adamant pickaxe', 'cost': 100, 'speed': 5}, 
'bronze pickaxe': {'name': 'bronze pickaxe', 'cost': 100, 'speed': 5}, 
'dragon pickaxe': {'name': 'dragon pickaxe', 'cost': 100, 'speed': 5}, 
'inferno adze': {'name': 'inferno pickaxe', 'cost': 100, 'speed': 5}, 
'iron pickaxe': {'name': 'iron pickaxe', 'cost': 100, 'speed': 5}, 
'mithril pickaxe': {'name': 'mithril pickaxe', 'cost': 100, 'speed': 5}, 
'rune pickaxe': {'name': 'rune pickaxe', 'cost': 100, 'speed': 5}, 
'steel pickaxe': {'name': 'steel pickaxe', 'cost': 100, 'speed': 5}} 

>>> for axe in Pickaxes.values(): 
    print '{name} - costs: {cost}, speed: {speed}'.format(**axe) 


bronze pickaxe - costs: 100, speed: 5 
mithril pickaxe - costs: 100, speed: 5 
adamant pickaxe - costs: 100, speed: 5 
steel pickaxe - costs: 100, speed: 5 
iron pickaxe - costs: 100, speed: 5 
rune pickaxe - costs: 100, speed: 5 
dragon pickaxe - costs: 100, speed: 5 
inferno pickaxe - costs: 100, speed: 5 
+0

那正是我想要的!非常感谢。如果你可以解释为什么你在Pickaxes.values()中写ax:和.format(** ax),那将会很棒 – Ali

+0

你应该阅读[Python Dictionaries](http://docs.python.org/2 /tutorial/datastructures.html#dictionaries)以及[Python字符串格式](http://docs.python.org/2/library/stdtypes.html#str.format)是如何工作的 –

2

提示:

  • 使用格式字符串得到一个不错的描述:

    '{name}: {value} coins'.format(name='adamant pickaxe', value=100) 
    
  • 叠代Pickaxes.iteritems()来获取所有的元素:

    for name, properties in Pickaxes.iteritems(): 
        # do something with name and properties['cost'] 
    
  • the sorted函数将帮助你以特定的顺序遍历你的镐。