[英]Can I print a function and set it to a variable at the same time in Python?
I'm wondering if it is possible to run the following piece of code while also capturing the resulting value into a variable at the same time. 我想知道是否可以运行以下代码,同时将结果值捕获到变量中。 The code is as follows:
代码如下:
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
The line of code is print "Question: ", word_drills[random.choice(keys)] . 代码行打印为“ Question:”,word_drills [random.choice(keys)] 。 I wanted to put this result in a variable and run an if/else statement to see if the answer is correct or not.
我想将此结果放入变量中并运行if / else语句以查看答案是否正确。 Thanks again.
再次感谢。
Er, yes? 嗯是吗 Just assign it to a variable:
只需将其分配给变量:
question = word_drills[random.choice(keys)]
print question
Now the variable question
remains available later. 现在,变量
question
稍后仍然可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.