In the following information, numbered points describe a core requirement of the program, and bullet points (in italics) are additional details, notes and hints regarding the requirement. Ask your tutor if you do not understand the requirements or would like further information.
1. Print a welcome message, and then prompt the user to select a difficulty by entering 1, 2 or 3.
2. Use a loop to re-prompt the user until a valid response (1, 2 or 3) is entered. Once a difficulty has been selected, print a message confirming the selected difficulty and set variables as follows:
3. Set a "score" variable to 0, and then enter a loop that repeats questions times.
The body of this loop must...
3.1. Print which question the user is up to out of the total number of questions, as well as how many lives that have remaining, e.g. "Question 1 of 5. You have 2 lives remaining."
3.2. If the current question is not the final question of the test, use the "ask_question" function (detailed below) to generate and administer a question involving numbers between 1 and max_num.
If the current question is the final question of the test, print "Challenge question!" and use the "ask_question" function (user defined) to generate and administer a question involving numbers between max_num and max_num multiplied by 2.
3.3. If the "ask_question" function returns a value of True, add one to the score variable. Otherwise, subtract 1 from the lives variable. If the lives variable is now 0, print "Out of lives, game over!" and immediately end the test (proceeding to Requirement 4).
4. Print a "test complete" message, followed by a message that displays the user's score out of questions, and what percentage that represents, e.g. "You scored 3/5 (60%)."
5. Print the user's grade, based upon their percentage. The grades are High Distinction (100-80%), Distinction (79-70%), Credit (69-60%), Pass (59-50%) and Fail (49-0%).
There are two points in Requirement 3.2 where the program must generate and administer a question. This is a self-contained task consisting of a number of steps, with the only difference being the minimum and maximum numbers to use. As such, it is ideal to create a function for this task.
You must create a function named "ask_question" that receives two parameters:
The function should generate two random integers between minimum and maximum, and then randomly select a mathematical operator of either '+' or '-'. It should use these values to display a question, e.g. "What is 4+ 5?", and prompt the user for their answer.
If the user answers correctly, the function should print "Correct!" and return the boolean value of True. Otherwise, the function should print "Incorrect!" and the correct answer, and return False.
The code that you design and write to implement this function is up to you, but you may find it useful to use the following C function:
The definition of the function should be at the start of the program, and it should be called where needed in the program. Revise Module 4 and 10 if you are uncertain about defining and using functions, and be sure to implement it so that it receives and returns values exactly as described above.
Ensure that the function does exactly what is specified above and nothing more - it is important to adhere to the stated specifications of a function.
Below are some minor additions and enhancements that you can make to the program to further test and demonstrate your programming ability.
To help you visualise the program, here is an example screenshot of the program being run:
Welcome to Maths Tester Pro.
Select a difficulty:
1) Easy
2) Medium
3) Hard
> easy
Invalid choice!
> 1
Enter 1, 2 or 3.
Easy mode selected!
Question 1 of 5. You have 3 lives remaining.
What is 5 - 32 2
Correct!
Question 2 of 5. You have 3 lives remaining.
What is 5 + 5? 10
Correct!
Question 3 of 5. You have 3 lives remaining.
What is 8 + 8? 19
Incorrect! The correct answer was 16.
Question 4 of 5. You have 2 lives remaining.
What is 1 + 2? 4
Incorrect! The correct answer was 3.
Question 5 of 5. You have 1 life remaining.
Challenge question!
What is 11 - 20? -9
Correct!
Test complete.
You scored 3/5 (60%).
Your grade is a Credit!