0

我想知道是否可以运行以下代码,同时将结果值捕获到变量中。代码如下:

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
    print correctvalue

代码行是print "Question: ", word_drills[random.choice(keys)]。我想把这个结果放在一个变量中并运行一个 if/else 语句来查看答案是否正确。再次感谢。

4

1 回答 1

3

呃,是吗?只需将其分配给一个变量:

question = word_drills[random.choice(keys)]
print question

现在该变量question稍后仍然可用。

于 2012-08-19T05:12:42.250 回答