2016-06-02 7 views
-5

申し訳ありませんが、私はこのばかげたエラーを私のコードで見つけました。他のstackoverflowエラーは役に立ちません。私は、特にdef makePoem()セクションで、このコードの何が問題なのかを理解する助けが必要です。TypeError: 'NoneType'

import random 

noun = ["fossil" , "horse" , "aardvark" , "chef" , "judge"] 
verb= [ "kicks", "jingles", "bounces", "slurps", "meows"] 
adjs= ["fury" , "balding" , "incredulous" , "fragant"] 
prep= ["against" , "after" , "into" , "beneath" , "for", "in"] 
ads= ["curiously" , "extravagantly" , "furiously" , "sensuously"] 

def selectn(list, n) : 
selection = [] 
while (len(selection) != n) : 
    w = random.choice(list) 
    if w not in selection : 
     selection.append(w) 
print(selection) 
# For some reason I'm getting TypeError: 'NoneType' object is not subscriptable for "makePoem()" 
def makePoem() : 
    my_nouns = selectn(noun,3) 
    my_verbs = selectn(verb,3) 
    my_adj = selectn(adjs,3) 
    my_adverb = selectn(ads,1) 
    my_prepo = selectn(prep,2) 

print ("A {} {}".format(my_adj[0], my_nouns[0])) 
print("") 
print("A {} {} {} {} the {} {}".format(my_adj[0], my_nouns[0], my_verbs[0], my_prepo[0], my_adj[1], my_nouns[1])) 
print("{}, the {} {}".format(my_adverb[0], my_nouns[0], my_verbs[1])) 
print("the {} {} {} a {} {}".format(my_nouns[1], my_verbs[2], my_prepo[1], my_adj[2], my_nouns[2])) 
makePoem()  

`

+0

エラーは何ですか? – pvg

+0

'selectn()'は何かを返すことになっていますか? –

+0

'selectn()'は文字列 'selection'を返します。 –

答えて

1

あなたが代わりにそれを印刷している、selectionを返していません。 print(selection)return selectionに置き換えてください。

+0

聖なるうわー!それはとても簡単な修正でした。 –

+0

@ CatGod6問題が解決した場合は、答えを記入してください。 –

+0

8分が過ぎるまでマークしませんが、私はします! –

関連する問題