python - How can I randomly choose a maths operator and ask recurring maths questions with it? -


I have a simple math task, I have a problem executing, which involves random import. I think 10 There is a quiz on randomly generated questions. Random me. Using the Randin function (0,12) to get the numbers, which works fine. I have the next bit of choosing a random operator, which I call ['+', '-', '*', '/ ']. Problems are coming with

, I have returned to the school more sophisticated coding, but it is my practice that I have the ability to create and ask only one question randomly, while also Decide whether it has been answered whether it is correct. Here's my code:

  import random ops = ['+', '-', '*', '/'] num1 = random.randint (0,12) num2 = random. Randint (0,10) Operation = Random.Search (OPS) Print (No. 1) Print (No. 2) Print (Operation) Mathematics = Number 1, Operation, Number 2 Print (Math)   < P> Right now though, my output is a bit messy, for example: <3> 6 * (3, '*', 6)   

clearly not set it by reply May be (3, '*', 6) I have a sub-routine in my other program Switch to will change, but it needs to first work!

And if I have not been very good then forgive me, this was the quick entertainment of that work school, and I am quite new with this limited knowledge, thanks in advance!

There is a function in Python called eval) which evaluates the string in which the mathematical expression.

  Import Random Ops = ['+', '-', '*', '/'] num1 = random.randint (0,12) num2 = random.randint (0, 10 ) Operation = Random. Chance (Ops) Print (Number 1) Print (Number 2) Print (Operation) Mathematics = eval (str (num1) + Operation + str (num2)) Print (Mathematics)   

You need to convert your numbers into strings because the function is expecting something like '4 * 2', '3 + 1', string etc.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -