Python可以编辑单个列表项吗?

时间:2014-03-01 06:48:04

标签: list python-2.7

我编写了一个程序,从列表中选择随机单词来制作句子。我想为此编写语法规则。现在我正在研究复数。

如果从list5中选择“那些”这个词,我想在“名词”中的所选单词的末尾添加一个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()

1 个答案:

答案 0 :(得分:0)

print方法中的result语句之前,添加:

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