2016-01-05 34 views
-1

我想知道如何使用我的triplet詩歌程序打印出句子。 我的程序會隨機挑選一個要使用的名詞列表。Python:如何打印我的簡單詩歌

我的程序:

import random 

def nouns(): 
    nounPersons = ["cow","crowd","hound","clown"]; 
    nounPlace = ["town","pound","battleground","playground"]; 
    rhymes = ["crowned","round","gowned","found","drowned"]; 

    nounPersons2 = ["dog","frog","hog"]; 
    nounPlace2 = ["fog","Prague","smog"]; 
    rhymes2 = ["log","eggnog","hotdog"]; 

    nounList1 = [nounPersons,nounPlace,rhymes] 
    nounList2 = [nounPersons2,nounPlace2,rhymes2] 
    nounsList = [nounList1, nounList2] 
    randomPick = random.choice(nounsList) 
    return(randomPick) 

verbs = ["walked","ran","rolled","biked","crawled"]; 
nouns() 

例如,我可以有「牛走到鎮上但隨後被淹死。」用隨機器替換名詞/韻(牛,鎮,淹死)和動詞(走路)。

我會以某種方式使用random.randint嗎?

我只是基本上需要一個通用的打印語句,就像我使用隨機數發生器在隨機選擇名詞/押韻時顯示的示例一樣。

+0

你可能想了解Python字符串格式化https://pyformat.info/ – lejlot

+1

'nounsList = [nounList1,N ounList2]'這不是你連接列表的方式。 –

+0

另外,不要用分號結束語句,換行符可以完成這項工作 – karlson

回答

1

像往常一樣(對我來說),有可能是一個更Python的方法,但讓你有什麼工作,我做了三件事情:

  1. 分配您的來電名詞()函數「 chosen_list'變量。這樣返回的'randomPick'被使用。

  2. 建在選擇步驟,以從「chosen_list」和你的動詞列表中列出了獲得個人的話

  3. 加入格式化最後print語句在組裝的話一句

代碼:

import random 
def nouns(): 

    nounPersons = ["cow","crowd","hound","clown"]; 
    nounPlace = ["town","pound","battleground","playground"]; 
    rhymes = ["crowned","round","gowned","found","drowned"]; 

    nounPersons2 = ["dog","frog","hog"]; 
    nounPlace2 = ["fog","Prague","smog"]; 
    rhymes2 = ["log","eggnog","hotdog"]; 

    nounList1 = [nounPersons,nounPlace,rhymes] 
    nounList2 = [nounPersons2,nounPlace2,rhymes2] 
    nounsList = [nounList1, nounList2] 
    randomPick = random.choice(nounsList) 

    return randomPick 

verbs = ["walked","ran","rolled","biked","crawled"] 

# this is change 1. 
chosen_list = nouns() 

# select single words from lists - this is change 2. 

noun_subj = random.choice(chosen_list[0]) 
noun_obj = random.choice(chosen_list[1]) 
rhyme_word = random.choice(chosen_list[2]) 
verb_word = random.choice(verbs) 

# insert words in to text line - this is change 3. 

print ("The {} {} to the {}. But then it was {}.".format(noun_subj, verb_word, noun_obj, rhyme_word))