开发者

In Python 2, creating a random question from shuffled numbers

I hope I can get some help with th开发者_StackOverflowis, 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!


from random import shuffle

numbers = [1, 75, 64, 80275, 2, 7]
shuffle(numbers)
print numbers
# [80275, 64, 75, 2, 7, 1]
question = "%d + %d / %d * %d - %d * %d" % tuple(numbers)
print question 
# 80275 + 64 / 75 * 2 - 7 * 1
answer = eval(question)
print answer
# 80269.7066667


def cheat_death(numbers):
    answer = eval('{0}+{1}/{2}*{3}-{4}*{5}'.format(*numbers))
    print "You have one chance to cheat death.\nTo do this, you must answer the following question:"
    print '{0}+{1}/{2}*{3}-{4}*{5}'.format(*numbers)
    user_answer = raw_input("> ")

    if user_answer == answer:
        start()
    else:
        dead()

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜