2016-06-02 3 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()  
당신은 selection 반환하지 않을

`

+0

무엇이 오류입니까? – pvg

+0

'selectn()'이 무언가를 돌려 주어야합니까? –

+0

'selectn()'은'selection' 문자열을 반환합니다. –

답변

1

대신을 인쇄하고 있습니다. print(selection)return selection으로 대체하십시오.

+0

이런 와우! 너무 쉽게 고칠 수있었습니다. –

+0

@ CatGod6 문제가 해결되면 답을 표시하십시오. –

+0

8 분이 지날 때까지 표시하지 않겠습니다. –