我一直在尝试在 python 中使用 dicts,所以我可以删除我认为它们被引用的“幻数”,但我不断收到错误:“NoneType”类型的对象没有 len()。它只在我的刽子手程序中的某些时候发生,我不确定为什么。这是我认为它崩溃的一般区域:
animals = 'ape bear beaver cat donkey fox goat llama monkey python bunny tiger wolf'.split()
colors = 'yellow red blue green orange purple pink brown black white gold'.split()
adjectives = 'happy sad lonely cold hot ugly pretty thin fat big small tall short'.split()
names = {'animals': animals, 'colors': colors, 'adjectives': adjectives}
categories = [names['animals'],names['colors'],names['adjectives']]
def getRandomWord(wordList):
wordList = random.choice(list(names.keys()))
wordIndex = random.randint(0,len(wordList))
print(animals[wordIndex])
if(random.choice(list(names.keys())) == 'animals'):
print("The category is animals!")
return animals[wordIndex]
if(random.choice(list(names.keys())) == 'colors'):
print("The category is colors!")
return colors[wordIndex]
if(random.choice(list(names.keys())) == 'adjectives'):
print("The category is adjectives!")
return adjectives[wordIndex]
我应该怎么做才能解决我的问题?