0

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

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

你会注意到我已经注释掉了我认为实际上会给我想要的结果的东西。不幸的是,每个人都没有。任何指导帮助将不胜感激。再次感谢。

更新

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

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。如果它是正确的,它会通知你,否则你是不正确的。但是不知道如何解决这个问题。只是想再次感谢大家的帮助,非常感谢。

4

2 回答 2

2

我想你想做的是这样的:

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

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

于 2012-08-18T22:38:42.533 回答
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.
于 2012-08-18T22:45:23.447 回答