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!