In this chapter, you learned how to generate random numbers and use the Math library. Let's put those skills to work on the MathContest game!

In this game, the user will be asked to correctly answer 4 different math problems. If the user answers one problem correctly, the next problem will be harder. Incorrect answers mean the next problem will be easier.

Game Overview
In this game, there are 4 possible types of math operations:

Addition - add two values (operand1 + operand2)
Subtraction - subtract two values (operand1 - operand2)
Multiplication - multiply two values (operand1 * operand2)
Square Root - find the square root of operand1
The "square root" of a number is the value that, when multiplied by itself, will produce that number. For example, here are some common square root values:

Square root of 4 is 2
Square root of 9 is 3
Square root of 16 is 4
Square root of 25 is 5
The square root of most numbers is a decimal value. For example, the square root of 3 is 1.732 and the square root of 12 is 3.464. However, for this game, we will use only integers, and the "correct" answer for any square root problem will be only the integer portion of the decimal value. So, for game purposes, the square root of 3 is 1, the square root of 12 is 3, and so on.

The sample runs below demonstrate how the program should work. In the first example, the user answers every problem correctly and scores 40 points (10 for each problem). Notice that the square root of 28 is 5, which is the integer portion of the decimal answer 5.215.

Enter game number: 1
2 - 5 = -3
CORRECT
17 + 16 = 33
CORRECT
Square Root of 28 = 5
CORRECT
70 + 11 = 81
CORRECT
Game Score: 40
In this example, the user gets two answers right and scores 20 points. The second answer is wrong (17 + 16 = 33) and the third answer is also wrong (square root of 7 is 2 in game terms).

Enter game number: 1
2 - 5 = -3
CORRECT
17 + 16 = 32
WRONG
Square Root of 7 = 3
WRONG
8 + 1 = 9
CORRECT
Game Score: 20
There are a couple of important game features not visible in the output. First, the game number is used to seed the random number generator. This ensures that every program run - with the same seed and user inputs - will produce the same output.

Seeding the random number generator allows you to repeatedly test your program with the same inputs until your code produces the expected output. It also allows our test cases to verify the entire program, so be sure to carefully follow the instructions for the randomization below.
Second, the game will adapt to the user's skill level! The two operands for each problem (e.g. 2 and 5 or 17 and 16) are randomly generated within a range. The starting range is 1 to 10. Each time the user gets a correct answer, the range doubles (2 to 20, 4 to 40, and so on). Each time the user gets a wrong answer, the range is halved (4 to 40 becomes 2 to 20, and so on). The minimum range is 1 to 10.

Starting Code
You will start with small parts of the program and will finish sections as specified below. If you are working offline, you can copy and paste this starting code into a "MathContest.py" source file that you create. But if you are working with CompuScholar's auto-graded system, you will find this code waiting for you when you click on the activity link in the course interface.

Do not change the names of any variables that have been declared for you. If you want to declare any additional variables as you complete the logic, you can choose those variable names.Detailed Program Requirements
Complete your program by carefully following the steps listed below. For the best grade, please match the expected output exactly (including correct spelling and case of all messages). Also be sure to use the correct math or random methods, where listed.

Add statements near the top to import the math and random libraries, which you will use later in the program.
Use the seed variable to seed the random number generator.
Create a for() loop that will iterate over the main game logic 4 times. Inside the loop:
Use the random number generator to store a random value between 0 and 3 (0, 1, 2, or 3) in the operation variable.
Use the random number generator to store a random value between rangeBottom and rangeTop (not including rangeTop itself) in the operand1 variable.
Use the random number generator to store a random value between rangeBottom and rangeTop (not including rangeTop itself) in the operand2 variable.
Complete the if() logic for the addition case. An addition problem should be shown if operation is 0.
Use input() to display the prompt "<operand1> + <operand2> = ", where <operand1> and <operand2> are the operand values. Remember, to add a number into a string, use str() to convert that number to a string first.
Convert the user's answer to an integer using int() and store it in a local variable.
If the user's answer is equal to operand1 + operand2, then set success to True.
Complete the if() logic for the subtraction case. A subtraction problem should be shown if operation is 1.
Use input() to display the prompt "<operand1> - <operand2> = ", where <operand1> and <operand2> are the operand values.
Convert the user's answer to an integer using int() and store it in a local variable.
If the user's answer is equal to operand1 - operand2, then set success to True.
Complete the if() logic for the multiplication case. A multiplication problem should be shown if operation is 2.
Use input() to display the prompt "<operand1>*<operand2> = ", where <operand1> and <operand2> are the operand values.
Convert the user's answer to an integer using int() and store it in a local variable.
If the user's answer is equal to operand1 * operand2, then set success to True.
Complete the if() logic for the square root case. A square root problem should be shown if operation is 3.
Use input() to display the prompt "Square root of <operand1> = ", where <operand1> is the operand value.
Convert the user's answer to an integer using int() and store it in a local variable.
Calculate the correct decimal answer using the math.sqrt() method. Then, get just the integer part of that answer using math.floor().
If the user's answer is equal to the integer part of the square root of operand1, then set success to True.
Complete the else logic when the user gets the wrong answer.
The rangeBottom variable should be set to half its current value, with a minimum of 1
The rangeTop variable should be set to half it's current value, with a minimum of 10
In both cases, use the max() method to select the larger of half the current range and either 1 (for rangeBottom) or 10 (for rangeTop)
You may find it useful to temporarily display the values in rangeBottom and rangeTop each time through the loop, to ensure your range update logic is working correctly. Remember to remove any extra print() statements before submitting for a grade1
You will need to complete most steps before running your program. However, you can use simple placeholder logic for steps 7 - 10 at first and focus on getting one type of math problem (e.g. addition) working at a time. Your program output will not match the examples until you complete all steps with an updated "success" variable and are making the correct updates to the rangeBottom and rangeTop values at the end of the loop.
Activity Results
The following sample runs can be used to validate your program. You should be able to produce this exact output for the given inputs. Your program should also produce the expected output for any other game numbers or combination of inputs.

Notice that for the same game seed, the user's correct or incorrect answers can change subsequent problems. Why? The random values are generated in a range and that range changes on each iteration (getting larger or smaller) based on the correct or incorrect results. The random generator may give different values based on what it has produced previously - that's OK! You will get repeatable results for the same combination of game number and specific user inputs.
Enter game number: 1
2 - 5 = 3
WRONG
8 + 8 = 16
CORRECT
Square Root of 14 = 3
CORRECT
35 + 5 = 41
WRONG
Game Score: 20
Enter game number: 1
2 - 5 = -3
CORRECT
17 + 16 = 33
CORRECT
Square Root of 28 = 6
WRONG
17 + 2 = 19
CORRECT
Game Score: 30
Enter game number: 2
2 + 2 = 4
CORRECT
7 * 11 = 77
CORRECT
17 * 6 = 104
WRONG
15 - 14 = 1
CORRECT
Game Score: 30
Enter game number: 5
6 * 9 = 0
WRONG
8 + 4 = 0
WRONG
3 + 2 = 0
WRONG
8 * 4 = 0
WRONG
Game Score: 0
When your program is complete and tested, submit it for grading!
Activity Rubric
If your project is being automatically graded by our system, your grade will be calculated from 0 to 100 as follows:

Points Description
3 math library imported
3 random library imported
7 for() loop set up
7 operation initialized with random value
7 operand1 initialized with random value between rangeBottom and rangeTop
7 operand2 initialized with random value between rangeBottom and rangeTop
7 math.sqrt() method used
7 math.floor() method used
7 max() method used
15 Test Case #1 passes
15 Test Case #2 passes
15 Test Case #3 passes
#import libraries

# Student Name

# seed random number generator
seed = int(input("Enter game number: "))
random.seed(seed)

# starting range for operands
rangeBottom = 1
rangeTop = 10

# starting score
score = 0

# loop over 4 game rounds
for i in range(4):
success = False # assume incorrect answer

operation = random.randint(0,3) # get random operation between 0 and 3
operand1 = random.randint(random_bottom, range_top) # get random operand1
operand2 = random.randint(random_bottom, range_top) # get random operand2

# display math problem, get user's answer, and set success = True if correct
if operation == 0: # addition (+)
user_answer = int(input(str(operand1) + " + " + str(operand2) + " = "))
success = True

elif operation == 1: # subraction (-)
user_answer = int(input(str(operand1) + " - " + str(operand2) + " = "))
if user_answer == operand1 - operand2:
success = True

elif operation == 2: # multiplication (*)
user_answer = int(input(str(operand1) + " * " + str(operand2) + " = "))
if user_answer == operand1 * operand2:
success = True

elif operation == 3: # square root
user_answer = int(input("Square root of " + str(operand1) + " = "))
correct_answer = math.floor(math.sqrt(operand1))
if user_answer == correct_answer:
success = True




if (success): # if user was right
rangeBottom *= 2 # double bottom range
rangeTop *= 2 # double top range
score += 10 # increase score
print("CORRECT")
else: # user was wrong
rangeBottom = max(range_bottom // 2, 1) # halve bottom range, minimum 1
rangeTop = max(range_top // 2, 10) # halve top range, minimum 10
print("WRONG")

print("Game Score: " + str(score))

Great job! Your code looks mostly correct, but there are a few minor issues that need to be fixed.

1. In the if-elif-else logic for the subtraction and multiplication cases, make sure the indentation is consistent. Right now, the nested if statements are not properly aligned.

2. The variables `range_bottom` and `range_top` are not consistent with the starting variable names `rangeBottom` and `rangeTop`. Update the variables to match the correct variable names.

3. Make sure to import the `random` library at the beginning of your code.

After making these adjustments, your program should run smoothly and pass all the test cases. Good luck!

Ther is something wrong with the code and I need you to fix it

Sure! I'll help you identify and fix the issues in your code. Let's review the code and make the necessary corrections:

```python
import random
import math

# seed random number generator
seed = int(input("Enter game number: "))
random.seed(seed)

# starting range for operands
rangeBottom = 1
rangeTop = 10

# starting score
score = 0

# loop over 4 game rounds
for i in range(4):
success = False # assume incorrect answer

operation = random.randint(0,3) # get random operation between 0 and 3
operand1 = random.randint(rangeBottom, rangeTop) # get random operand1
operand2 = random.randint(rangeBottom, rangeTop) # get random operand2

# display math problem, get user's answer, and set success = True if correct
if operation == 0: # addition (+)
user_answer = int(input(str(operand1) + " + " + str(operand2) + " = "))
if user_answer == operand1 + operand2:
success = True

elif operation == 1: # subtraction (-)
user_answer = int(input(str(operand1) + " - " + str(operand2) + " = "))
if user_answer == operand1 - operand2:
success = True

elif operation == 2: # multiplication (*)
user_answer = int(input(str(operand1) + " * " + str(operand2) + " = "))
if user_answer == operand1 * operand2:
success = True

elif operation == 3: # square root
user_answer = int(input("Square root of " + str(operand1) + " = "))
correct_answer = math.floor(math.sqrt(operand1))
if user_answer == correct_answer:
success = True

if success: # if user was right
rangeBottom *= 2 # double bottom range
rangeTop *= 2 # double top range
score += 10 # increase score
print("CORRECT")
else: # user was wrong
rangeBottom = max(rangeBottom // 2, 1) # halve bottom range, minimum 1
rangeTop = max(rangeTop // 2, 10) # halve top range, minimum 10
print("WRONG")

print("Game Score: " + str(score))
```

I have made the following corrections to your code:

1. Corrected the variable names `range_bottom` and `range_top` to `rangeBottom` and `rangeTop`.
2. Adjusted the indentation for the subtraction and multiplication cases.
3. Added the missing import statements for the `random` and `math` libraries.

Please try running the corrected code now and see if it functions as expected. Let me know if you encounter any further issues!