Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Right now, you're drawing a different category three times. The first time you compare it to <code>'animals'</code>; the second to <code>'colors'</code>; and the third to <code>'adjectives'</code>. </p> <p>But what if you drew <code>'colors'</code> and then <code>'animals'</code> and then <code>'colors'</code> again? None of your three "if" branches will trigger. Since there's no <code>return</code> which is executed, there's an effective <code>return None</code> at the end.</p> <p>Alternatively, if your <code>names</code> isn't what you think it is, you could be drawing a <code>None</code> value, I guess: you didn't show the whole stacktrace, so I'm guessing about where the error manifests.</p> <p>(Also, your <code>wordIndex</code> could possibly be set to <code>len(wordList)</code>, which would result in an IndexError because the last index is <code>len(wordList)-1</code>. Moreover, since the <code>wordIndex</code> didn't necessarily come from the list associated with the category, you could have indexing errors there too.)</p> <p>I think you can simplify your code to something like:</p> <pre><code>import random 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} def getRandomWord(words_by_category): category = random.choice(list(words_by_category.keys())) print("the category is", category) wordlist = words_by_category[category] chosen_word = random.choice(wordlist) return chosen_word </code></pre> <p>after which we have:</p> <pre><code>&gt;&gt;&gt; getRandomWord(names) the category is colors 'blue' &gt;&gt;&gt; getRandomWord(names) the category is adjectives 'happy' &gt;&gt;&gt; getRandomWord(names) the category is colors 'green' &gt;&gt;&gt; getRandomWord(names) the category is animals 'donkey' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload