Program Output Example

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! Enter 1, 2, or 3.
> 1
Easy mode selected!

Question 1 of 5. You have 3 lives remaining.
What is 5 - 10? -5
Correct!

Question 2 of 5. You have 3 lives remaining.
What is 5 - 3? 2
Correct!

Question 3 of 5. You have 3 lives remaining.
What is 3 + 6? 9
Correct!

Question 4 of 5. You have 3 lives remaining.
What is 4 - 10? 14
Incorrect! The correct answer was -6.

Question 5 of 5. You have 2 lives remaining.
Challenge question!
What is 12 - 11? 1
Correct!

Test complete.
You scored 4/5 (80%)
You passed!

The program welcomes the user and then prompts them to select a difficulty. The user first tried entering "easy" and was told it was an invalid choice. They enter 1 and the program confirms that they have selected Easy mode. The difficulty determines the number of questions, largest number used when generating a question, and number of questions that the user can get wrong.

The program then generated 5 random questions (the last one being a challenge question with larger numbers) and presented them to the user, who answered 4 out of 5 of them correctly.

Finally, the program displayed the overall score and percentage, and tells the user that they passed.

Pseudocode

As emphasised by the case study of Module 5, it is important to take the time to properly design a solution before starting to write code. Hence, this assignment requires you to write and submit pseudocode of your program design as well as the code for the program. Furthermore, while your tutors are happy to provide help and feedback on your assignment work throughout the semester, they will expect you to be able to show your pseudocode and explain the design of your code.

You will gain a lot more benefit from pseudocode if you actually attempt it before trying to code your program - even if you just start with a rough draft to establish the overall program structure, and then revise and refine it as you work on the code. This back and forth cycle of designing and coding is completely normal and expected, particularly when you are new to programming. The requirements detailed on the following pages should give you a good idea of the structure of the program, allowing you to make a start on designing your solution in pseudocode.

See Reading 3.3 and the discussion board for further advice and tips regarding writing pseudocode.

Write a separate section of pseudocode for each function you define in your program so that the pseudocode for the main part of your program is not cluttered with function definitions. Ensure that the pseudocode for each of your functions clearly describes the parameters that the function receives and what the function returns back to the program. Pseudocode for functions should be presented after the pseudocode for the main part of your program.

It may help to think of the pseudocode of your program as the content of a book, and the pseudocode of functions as its appendices: It should be possible to read and understand a book without necessarily reading the appendices, however they are there for further reference if needed.

Only one function is required in this assignment (detailed later in the assignment brief).

Program Requirements

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:

  • If "Easy" (1) was chosen lives = 3, max_num = 10 and questions = 5
  • If "Medium" (2) was chosen lives = 2, max_num = 25 and questions = 10
  • If "Hard" (3) was chosen lives = 1, max_num = 50 and questions = 15
  • "lives" represents how many incorrect answers are permitted, max_num represents the largest number used when generating a question, and questions represents the number of questions.

3. Set a "score" variable to 0, and then enter a loop that repeats questions times.

  • "score" will be used to keep track of how many questions the user has answered correctly.

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 to generate and administer a question involving numbers between max_num and max_num multiplied by 2.

  • e.g. The challenge question on Easy difficulty would use numbers between 10 and 20.

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%).

  • Round the percentage value to the nearest whole number.

5. If the user obtained at least 50% in the test, print "You passed!", otherwise print You failed!

The "ask_question" Function

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:

  • "minimum", an integer representing the smallest number to use in the question
  • "maximum", an integer representing the largest number to use in the question

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 two functions:

  • The "random.randint()" function, to generate random numbers within a range
  • The "eval()" built-in function, to evaluate a string as a Python expression, e.g. If you have a string variable named text that contains '4 + 5', then eval(text) will return 9

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 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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.