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

2024-04-19 21:30:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

代码行是打印“问题:”,单词\u[随机选择(键)]。我想把这个结果放到一个变量中,然后运行if/else语句,看看答案是否正确。再次感谢。你知道吗


Tags: the代码infromisrandomkeyslist