2012-08-18 69 views
0

所以正如我之前所说的,我正在努力尝试进行多项选择测验。测验会随机抓取3把钥匙作为“答案”。然后,测验将采用3个选定键的值并将其用作“问题”。我试图利用random.sample作为参数从随机选择的键值中选择一个值。我的代码如下:从Python词典中的随机键中选择一个随机值

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() 

你会注意到我已经评论了我以为会给我想要的结果。不幸的是,每一个都没有。任何指导援助将不胜感激。再次感谢。

UPDATE

所以我已经把提供新的信息。再次感谢你。这是我想出了:

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, & 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("> ") 
    print selection 

nodupchoice() 

我想现在破译将是问题:如何检查针对的是在字典word_drills用户选择。我打算使用if/else。如果它是正确的,它会通知你,否则你是不正确的。不知道如何处理这个问题。只是想再次感谢大家的帮助,这绝对是值得赞赏的。

回答

1

您可能需要使用random.choice()选择键以尝试:

keys = random.sample(word_drills, 3) 
print keys 
print word_drills[random.choice(keys)] 

输出:

['has-a', 'attribute', 'class'] 
A phrase to say that something is composed of other things or has a trait, as in a Salmon *** mouth. 
+1

我不明白listcomp在这里扮演什么角色。不会'lst'总是等于'random.sample(word_drills,3)'? – DSM

+0

是的,它会的。在这种情况下,它只是一个简单的手段,使它更易于使用random.choice()并打印所有选定的键。 –

+1

我想DMS的意思是:'random.sample'已经返回一个列表/可迭代的,所以它不必再被包装。 –

2

我猜测你想要做的是这样的:

print word_drills[random.choice([key1, key2, key3])] 

这将打印属于随机抽样的关键之一的值之一。

+0

我想程序随机选择键1,键2,和KEY3的一个值。这样,我可以保证何时显示价值(或问题),其相应的键(或答案)已经作为可能的选择出现。 – kiddsupreme

+0

这会工作,但我认为可能有一个结束括号''key3]' –

+0

丢失是的,谢谢。 –