我可以在Python中打印函数并同时将其设置为变量吗?
我想知道是否可以运行下面的代码,同时将结果值捕获到变量中。代码如下:我可以在Python中打印函数并同时将其设置为变量吗?,python,function,dictionary,python-2.7,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.
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\u演练[随机选择(键)]。我想把这个结果放在一个变量中,然后运行if/else语句,看看答案是否正确。再次感谢。呃,是吗?只需将其分配给一个变量:
question = word_drills[random.choice(keys)]
print question
现在变量
问题
稍后仍然可用。呃,是吗?只需将其分配给一个变量:
question = word_drills[random.choice(keys)]
print question
现在变量
问题
稍后仍然可用。为什么不能将其设置为变量然后打印?为什么不能将其设置为变量然后打印?