Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect a random value from a selection of random keys from a Python dictionary
    text
    copied!<p>So as I stated before, I'm working on trying to make a multiple choice quiz. The quiz would grab 3 keys at random as the "answers". Then the quiz would take the value of the 3 selected keys and utilize it as a "question." I'm trying to utilize random.sample as an argument to select a value from randomly selected key values. My code is below:</p> <pre><code>import random word_drills = {'class': 'Tell Python to make a new kind of thing.', 'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.', 'instance': 'What you get when you tell Python to create a class.', 'def': 'How you define a function inside a class.', 'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.', 'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.', 'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.', 'attribute': 'A property classes have that are from composition and are usually variables.', 'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish', 'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'} def nodupchoice(): key1, key2, key3 = random.sample(word_drills, 3) print "%s, %s, %s" % (key1, key2, key3) #print word_drills.random.sample[key1, key2, key3] #print word_drills[key1] #print word_drills[random.sample(key1, key2, key3)] nodupchoice() </code></pre> <p>You will notice that I have commented out what I thought would actually give me the results I desired. Unfortunately, each one did not. Any assistance of guidance would be appreciated. Thanks again.</p> <p><em><strong>UPDATE</em></strong></p> <p>So I have incorporated the new information provided. Thank you again. This is what I came up with:</p> <pre><code>import random word_drills = {'class': 'Tell Python to make a new kind of thing.', 'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.', 'instance': 'What you get when you tell Python to create a class.', 'def': 'How you define a function inside a class.', 'self': 'Inside the functions in a class, self is a variable for the instance/object being accessed.', 'inheritance': 'The concept that one class can inherit traits from another class, much like you and your parents.', 'composition': 'The concept that a class can be composed of other classes as parts, much like how a car has wheels.', 'attribute': 'A property classes have that are from composition and are usually variables.', 'is-a': 'A phrase to say that something inherits from another, as in a Salmon *** Fish', 'has-a': 'A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth.'} def nodupchoice(): # For loop that creates a list named keys. It grabs 3 random keys from the dictionary word_drills keys = [x for x in random.sample(word_drills, 3)] # User is presented with a question. A value from the previous randomly selected keys is selected as the 'question' print "Question: ", word_drills[random.choice(keys)] # Set the variables key1, key2, &amp; key3 to the 3 keys in the list 'keys' key1, key2, key3 = keys[0], keys[1], keys[2] # User is presented with 3 choices. print "\n\n(a)%s (b)%s (c)%s" % (key1, key2, key3) selection = raw_input("&gt; ") print selection nodupchoice() </code></pre> <p>The issue I'm trying to decipher now would be: How to check the users selection against what is in the dictionary word_drills. I was planning on using an if/else. If it was correct, it would notify you, else you were incorrect. Not sure how to approach this however. Just wanted to thank everyone again for the assistance, it is definitely appreciated.</p>
 

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