在 Python 2 中,从打乱的数字中创建一个随机问题
我希望我能得到一些帮助,我在网上查找完全没有运气。好吧,事实上我是 Python 新手。
我正在学习 Python The Hard Way,并且我真的开始喜欢 Python。于是我做了一个简单的小游戏。我正在寻找一种“欺骗死亡”的方法,我的想法是,为了逃脱死亡,你需要回答一个数学问题,如果你答对了,你会回到start(),或者如果你点错了,你就会去 dead()。所以这是到目前为止我针对这个问题的代码:
from random import shuffle
numbers = [1, 75, 64, 80275, 2, 7]
shuffle(numbers)
def question(numbers):
现在,从这里开始,使用我的数字列表,我不太知道如何导入打乱的数字。我想有一个这样的预设问题:
__ + __ / __ * __ - __ * __
这样它就会引入已洗牌的数字列表,然后用 __ 替换问题中相应的 __ 。然后,我会:
print "Your answer:"
user_answer = raw_input("> ")
这样他们就可以输入他们的答案。之后,我需要一种方法来验证答案,所以我会这样做:
if useranswer == answer:
print "You lived!"
start()
else:
dead()
变量“answer”是 python 将返回的答案。所以,最后,我认为代码应该如下所示:
from random import shuffle
numbers = [1, 75, 64, 80275, 2, 7]
question = shuffle(numbers)
def cheat_death(numbers):
answer = __ + __ / __ * __ - __ * __
print "You have one chance to cheat death.\nTo do this, you must answer the following question:"
print question
user_answer = raw_input("> ")
if user_answer == answer:
start()
else:
dead()
好的,我有一段工作代码。它生成随机数,然后将它们放入问题中。这是代码:
i = 0
numbers = []
while i < 6:
numbers.append(random.randrange(1,900))
i = i + 1
def cheat_death(numbers):
shuffle(numbers)
question = "%d + %d / %d * %d - %d * %d" % tuple(numbers)
print "You have a single chance to cheat death. To live, please answer the question correctly below:"
print question
answer = eval(question)
user_answer = raw_input("> ")
if user_answer == answer:
start()
else:
dead()
cheat_death()
但是每次我输入答案时,无论它是否正确,它都会说它是错误的。这可能是因为 eval(question) 吗?或者我只是不知道!
I hope I can get some help with this, I've had absolutely no luck looking online. well that and the fact I'm new to Python.
I'm going through Learn Python The Hard Way, and am really starting to like python. So I made a simple little game. I'm looking for a way to "cheat death", and my idea is that to escape death, you need to answer a math question, and if you get it right, you'd go back to start(), or if you dot it wrong, you'd go to dead(). So here's the code that I have for this question so far:
from random import shuffle
numbers = [1, 75, 64, 80275, 2, 7]
shuffle(numbers)
def question(numbers):
Now from here, using my list of numbers, I con't quite know how to import the shuffled numbers. I am thinking to have a preset question like this:
__ + __ / __ * __ - __ * __
So that it'll bring in the list of numbers that've been shuffled, and then substitute __ for the corresponding __ in the question. Then, I'll have:
print "Your answer:"
user_answer = raw_input("> ")
So they can put in their answer. After this, I will need a way to verify the answer, so I'll do this:
if useranswer == answer:
print "You lived!"
start()
else:
dead()
Where the variable 'answer' is what python will return as the answer. So, at the end, here's something I think the code should look like:
from random import shuffle
numbers = [1, 75, 64, 80275, 2, 7]
question = shuffle(numbers)
def cheat_death(numbers):
answer = __ + __ / __ * __ - __ * __
print "You have one chance to cheat death.\nTo do this, you must answer the following question:"
print question
user_answer = raw_input("> ")
if user_answer == answer:
start()
else:
dead()
Ok, I have a working piece of code. It generates random numbers, and then puts them into a question. here's the code:
i = 0
numbers = []
while i < 6:
numbers.append(random.randrange(1,900))
i = i + 1
def cheat_death(numbers):
shuffle(numbers)
question = "%d + %d / %d * %d - %d * %d" % tuple(numbers)
print "You have a single chance to cheat death. To live, please answer the question correctly below:"
print question
answer = eval(question)
user_answer = raw_input("> ")
if user_answer == answer:
start()
else:
dead()
cheat_death()
But every single time I enter an answer, whether it's right or not, it says it's wrong. Could this be because of the eval(question)? Or man I just don't know!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想这就是你所需要的。每次调用时,您可能希望再次对数字进行洗牌,或者甚至可以添加一个函数来随机生成特定范围内的数字(以及可能的问题)。
I think this is what you would need. Upon each call, you would likely want to shuffle the numbers again, or could even add a function that randomly generates the numbers (and likely the questions) within certain ranges.