2013-12-23 40 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函數將幫助你以特定的順序遍歷你的鎬。