これは文章生成プログラムのコードです。それは、オプションである前置詞句を作成するようにユーザーに求めます(特定の確率で出現する可能性があります)。句をオプションにする方法はわかりません。文章生成プログラムを任意にする
import random
articles = ("A", "THE")
nouns = ("BOY", "GIRL", "BAT", "BALL",)
verbs = ("HIT", "SAW", "LIKED")
prepositions = ("WITH", "BY")
def sentence():
"""Builds and returns a sentence."""
return nounPhrase() + " " + verbPhrase()
def nounPhrase():
"""Builds and returns a noun phrase."""
return random.choice(articles) + " " + random.choice(nouns)
def verbPhrase():
"""Builds and returns a verb phrase."""
return random.choice(verbs) + " " + nounPhrase() + " " + prepositionalPhrase()
def prepositionalPhrase():
"""Builds and returns a prepositional phrase."""
return random.choice(prepositions)+ " " + nounPhrase()
def main():
"""Allows the user to input the number of sentences to generate."""
number = input("Enter the number of sentences: ")
for count in xrange(0, number):
print sentence()
main()