Not Complete

This challenge is a based off a question from one of the followers on Master Code Online. You can see their question at https://www.mastercode.online/questions/question/57/.

We ask that you build a math game. This game will pick two random numbers and a random operator. The users will be asked to either add the numbers together, find the difference, multiply or divide. We will keep the score on the player's success. The game should go through 5 rounds.

Below you will find how the game should look in the terminal. Let's get to coding and let us know how your game turns out.

```
How well do you know math?
What is 7 - 2
5
Correct!
What is 10 / 5
2
Correct!
What is 3 + 7
10
Correct!
What is 4 + 7
11
Correct!
What is 5 / 6
1
Incorrect
Your score is 4
```

We will use two modules to solve this challenge. We will use random which will allow us to randomly generate the problem. Another module we will use is the operator module. This will allow us to change out the operators used in the equation.

We will also need three functions to make this work. The first function will generate the question, the second function will check if the answer is correct for the randomly generated function and lastly the final function will return a message to the user and keep score. Let's get started.

**1.** Let's start by importing the modules.

```
import random
import operator
```

**2**. Now we will create the function that will create the random math question.

```
import random
import operator
def random_problem():
```

**3**. As mentioned before we want to have random addition, subtraction, multiplication and division problems for our program. To manage this we need to put all the available operators in a dictionary and then we can randomly select from this data container.

```
import random
import operator
def random_problem():
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
}
```

**4. **Now we will randomly generate our two numbers.

```
import random
import operator
def random_problem():
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
}
num_1 = random.randint(1, 10)
num_2 = random.randint(1, 10)
```

**5**. Now we will randomly select the operator. We use the random.choice() method to randomly select an operator. We need to put the operations into a list since choice() only take a sequence data type.

```
import random
import operator
def random_problem():
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
}
num_1 = random.randint(1, 10)
num_2 = random.randint(1, 10)
operation = random.choice(list(operators.keys()))
```

**6**. Now that we have two numbers and operator we can now perform the math operation to get the answer. We will do this by using the get method than applying that to the operator.

```
import random
import operator
def random_problem():
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
}
num_1 = random.randint(1, 10)
num_2 = random.randint(1, 10)
operation = random.choice(list(operators.keys()))
answer = operators.get(operation)(num_1, num_2)
```

**7**. Now we will return a message to the user. This message will show the equation we want them to perform.

```
import random
import operator
def random_problem():
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
}
num_1 = random.randint(1, 10)
num_2 = random.randint(1, 10)
operation = random.choice(list(operators.keys()))
answer = operators.get(operation)(num_1, num_2)
print(f'What is {num_1} {operation} {num_2}')
return answer
```

**8**. Now we will create a function that will handle asking the question and then the function will return True or False based on users answer.

`def ask_question():`

**9**. We will call the random_problem() function first to run that function.

```
def ask_question():
answer = random_problem()
```

**10**. We will now get the users to answer the question via an input. We need to convert

```
def ask_question():
answer = random_problem()
guess = float(input())
```

**11**. Now we will compare the return from the random_problem() function and guess input to see if they match. If they do we will return True and if Not we will return False. This will help with the next function.

```
def ask_question():
answer = random_problem()
guess = float(input())
return guess == answer
```

**12**. We will now create a function that will handle running the game and keeping score. We will call this function game().

`def game():`

**13.** We will start the game off with a simple message to the game player.

```
def game():
print("How well do you know math?\n")
```

**14**. Now we will initialize a variable to hold our score.

```
def game():
print("How well do you know math?\n")
score = 0
```

**15**. We will now set how questions the game will ask before it is complete. We use the range function. You can change the number in the range function to anything you want.

```
def game():
print("How well do you know math?\n")
score = 0
for i in range(5):
```

**16**. We will now check if the user's answer is correct for the asked question.

```
def game():
print("How well do you know math?\n")
score = 0
for i in range(5):
if ask_question() == True:
```

**17**. If the user answers correctly we will return a message and add one to the score value.

```
def game():
print("How well do you know math?\n")
score = 0
for i in range(5):
if ask_question() == True:
score += 1
print("Correct!")
```

**18**. We will now handle what happens when the user gets the question wrong. In this case, we will only return a message.

```
def game():
print("How well do you know math?\n")
score = 0
for i in range(5):
if ask_question() == True:
score += 1
print("Correct!")
else:
print("Incorrect")
```

**19**. At the end of the game, we will return a message to the user informing the user how many questions they got right.

```
def game():
print("How well do you know math?\n")
score = 0
for i in range(5):
if ask_question() == True:
score += 1
print("Correct!")
else:
print("Incorrect")
print(f"Your score is {score}")
```

**20**. Let's now call the game function which will run the whole algorithm for this game to work.

```
def game():
print("How well do you know math?\n")
score = 0
for i in range(5):
if ask_question() == True:
score += 1
print("Correct!")
else:
print("Incorrect")
print(f"Your score is {score}")
game()
```

**21.** Here is the final game!

```
import random
import operator
def random_problem():
operators = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.truediv,
}
num_1 = random.randint(1, 10)
num_2 = random.randint(1, 10)
operation = random.choice(list(operators.keys()))
answer = operators.get(operation)(num_1, num_2)
print(f'What is {num_1} {operation} {num_2}')
return answer
def ask_question():
answer = random_problem()
guess = float(input())
return guess == answer
def game():
print("How well do you know math?\n")
score = 0
for i in range(5):
if ask_question() == True:
score += 1
print("Correct!")
else:
print("Incorrect")
print(f"Your score is {score}")
game()
```

We were able to help our friend out with his game. We have more plans for this game in the future challenges we suggest you hold on to this one! If you have any questions please let us know.