我寫了一個程序,從列表中選擇隨機單詞來製作一個句子。我想爲此編寫語法規則。現在我正在研究複數。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()
這是一個小比 「編輯」 更復雜。您的數據結構需要重大修改。 –
也有更多描述性的名字。 –