2014-03-01 63 views
0

我寫了一個程序,從列表中選擇隨機單詞來製作一個句子。我想爲此編寫語法規則。現在我正在研究複數。Python能編輯單個列表項嗎?

如果從list5中選擇了「those」這個單詞,我想在'名詞'中選擇一個單詞的末尾添加一個s或es。

import random 
class Wrds(object): 

    verbs = [ 
     "walk", "run"   
    ] 

    pronouns = [ 
     "I", "you" 
    ] 

    help_verbs = [ 
     "will", "might" 
    ] 

    nouns = [ 
    "boy", "girl" 
    ] 

    list5 = [ 
    "the", "that", "this", "a", "those" 
    ] 

    punctuation = [ 
     ".", "?", "!" 
    ] 

    def result(self): 
     a = random.choice(Wrds.verbs) 
     b = random.choice(Wrds.pronouns) 
     c = random.choice(Wrds.help_verbs) 
     d = random.choice(Wrds.nouns) 
     e = random.choice(Wrds.list5) 
     f = random.choice(Wrds.punctuation)  
     print "%s %s %s %s %s%s" % (b, c, a, e, d, f) 

def ask(): 

    a = raw_input("> ") 
    if a == "go": 
     w = Wrds() 
     return w.result() 

    elif a == "exit": 
     exit() 

while True:   
    ask() 
+2

這是一個小比 「編輯」 更復雜。您的數據結構需要重大修改。 –

+0

也有更多描述性的名字。 –

回答

0

result方法print聲明之前,添加:

if e == 'those': 
     d += 's' 
+0

直到添加「這些」才能正常工作。 –

+0

'如果在('那些','這些'):' – John1024

+0

這需要更改代碼。總是一件壞事。 –