In Python 2, creating a random question from shuffled numbers

I hope I can work with this, I was not lucky to watch online. well, and the fact that I'm new to Python.

I am experiencing Learn Python The Hard Way, and I'm really starting to love python. So I made a simple game. Iโ€™m looking for a way to โ€œcheat deathโ€, and my idea is to avoid death, you need to answer a mathematical question, and if you fix it, you will return to the beginning (), or if you do not make a mistake, you will go to dead () . So here is the code that I have for this question:

from random import shuffle numbers = [1, 75, 64, 80275, 2, 7] shuffle(numbers) def question(numbers): 

Now, using my list of numbers, I don't know how to import shuffled numbers. I think I have a question asked:

 __ + __ / __ * __ - __ * __ 

For him to list the numbers that were shuffled, then replace __ for the corresponding __ question. Then I will have:

 print "Your answer:" user_answer = raw_input("> ") 

That way they can answer them. After that, I will need a way to check the answer, so I will do the following:

 if useranswer == answer: print "You lived!" start() else: dead() 

Where the variable "response" is what python will return as an answer. So in the end, here is what I think the code should look like this:

 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 working code. It generates random numbers and then puts them into question. here is 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 time I enter the answer, is it right or wrong, he says that it is wrong. Could this be due to eval (question)? Or the person I just don't know!

+4
source share
2 answers
 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 
+2
source
 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 need. With each call, you probably want to shuffle the numbers again, or you can even add a function that randomly generates numbers (and probably questions) in certain ranges.

0
source

All Articles