我可以在Python中同时打印函数并将其设置为变量吗?

时间:2012-08-19 05:07:38

标签: python function dictionary python-2.7

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

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

代码行是打印“问题:”,word_drills [random.choice(keys)] 。我想将此结果放在变量中并运行if / else语句以查看答案是否正确。再次感谢。

1 个答案:

答案 0 :(得分:3)

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

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

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