2012-03-17 41 views

回答

0

嗯,我第一次寫這個,但鮑里斯的更簡潔,所以我決定不提交......但對於初學者來說可能會更易讀。

quality_list = ['Flaming', 'Frozen', 'etc.'] 
quality = random.choice(quality_list) 

item_list = ['Sword', 'Dagger', 'Mace', 'etc.'] 
item = random.choice(item_list) 


hot_qualities = ['Flaming', ...] 
cold_qualities = ['Frozen', ...] 
hot_elements = ['Fire', 'Inferno'] 
cold_elements = ['Cold', 'Frost'] 

if quality in hot_qualities: 
    element = random.choice(hot_elements) 
elif quality in cold_qualities: 
    element = random.choice(cold_elements) 

# NB this is Python 2.X's print and string format, adjust accordingly for 3.X 
print "%s %s of %s" % (quality, item, element) 

雖然在實際項目中我會像鮑里斯那樣進一步概括它。

+0

猜測我可能會啓動一個Python課程:) 至於關鍵字 - 閱讀[this](http://pyref.infogami.com/in)或向下滾動到'in and not in'in [Python reference](http://docs.python.org/reference/expressions.html#not -在)。 – egor83 2012-03-17 12:27:28

5

怎麼樣這類的解決方案:你構建地圖形容詞和名詞的可能的組合,並選擇其中的一種:

weapon_map = {"Flaming": ["Fire", "Inferno"], "Frozen": ["Cold", "Frost"]} 
selection = random.choice(weapon_map.items()) 
print (selection[0] + " of " + random.choice(selection[1])) 

這種做法更容易維護和理解。

+0

太糟糕了我從來沒有見過任何你剛纔寫的東西(除了「打印」)後,一段時間的學習蟒蛇(與老師),實際編程課程甚至開始XD。甚至不知道「哈希」是什麼。 – user1275670 2012-03-17 12:03:14

+1

@ user1275670:然後我會說http://www.google.com/search?q=python+tutorial可能是一個開始:) – 2012-03-17 12:07:03

相關問題