# Make python ask questions from strings

I want to make python ask questions to the user - from random variables in lists. It needs to ask the question requiring an input from the user.

This is my code so far:

import time import random question = "0" score = "0" name = input("What is your full name?") print ("Hello " + name, "welcome to The Arithmetic Quiz") time.sleep(2) numbers = list(range(1, 50)) operators = ["+", "-", "*"] numbers1 = list(range(1,10)) print(str(random.choice(numbers)) + random.choice(operators) + str(random.choice(numbers1)))`

How would I make the last line of code ask a question and get an input from the user?

Also how would I make it so that python says whether this is correct when I do not know what python will ask?

## Answers

The answer is already in your code.

user_input = input(str(random.choice(numbers)) + random.choice(operators) + str(random.choice(numbers)) + "? ") should work.

It gets a sample random number from numbers, gets a random operator from operators, gets another random number from numbers, and stores the input to the variable user_input.

To get Python to check your answer, store the randomly generated arguments inside variables and check them. (If there is a better way of doing this, I would appreciate it if someone pointed it out to me).

operand1 = random.choice(numbers) operand2 = random.choice(numbers) operator = random.choice(operators) if operator == '+': answer = operand1 + operand2 elif operator == '-': answer = operand1 - operand2 else: answer = operand1 * operand2 user_input = input(str(operand1) + operator + str(operand2) + "? ") if str(answer) == user_input: print('Correct!') else: print('Wrong!')

EDIT: @mhawke's answer has a better way of storing and manipulating the operands. Instead of storing the operators in a list, store them in a dict and map them to their corresponding operator function as so:

import operator operators = {"+": operator.add, "-": operator.sub, "*": operator.mul} operand1 = random.choice(numbers) operand2 = random.choice(numbers) op = random.choice(operators) expected_answer = op(operand1, operand2)

Documentation for operator.

For the second part of your question, how to determine whether the user entered the correct answer, you can store the randomly selected values and evaluate the resulting expression. Then compare that to the user's value:

import operator operators = {"+": operator.add, "-": operator.sub, "*": operator.mul} operand1 = random.choice(numbers) operand2 = random.choice(numbers1) op = random.choice(operators) expected_answer = op(operand1, operand2) user_answer = input('{} {} {} = ?: '.format(operand1, op, operand2) if int(user_answer) == expected_answer: print('Correct!') else: print('Wrong. The correct answer is {}'.format(expected_answer)

The operators are stored in a dictionary. The operator tokens (+, -, *) are the keys in this dictionary, and the values are functions from the operator module that perform the operations. Using a dictionary like this is very flexible because if you wanted to support a new operator, e.g. division, you can just add it to the operators dictionary:

operators = {"+": operator.add, "-": operator.sub, "*": operator.mul, '/': operators.div}

### Need Your Help

### Template matching algorithms

algorithm image-processing computer-vision template-matching

Please suggest any template matching algorithms, which are independent of size and rotation.