Functions
There is an expectation that your solution should use functionA section of code that, when programming, can be called by another part of the program with the purpose of returning one single value.. By defining your own functions, you will be able to reuse code throughout your program. It is important you provide adequate evidence of how and why you used functions in your solution.
Functions should include:
- user defined functions
- in-built functions
- mathematical functions
You should attempt to use higher order functionsHigher order functions operate on other functions, either by taking them as arguments or by returning them as a result..
Python implementation
58 def quizCreator(choice):
59 '''function to create the questions for the quiz'''
60 if choice == 1:#If easy questions picked create 5 addition & 5 subtraction questions
61 for i in range(5):
62 addQuestion()
63 for i in range(5):
64 subQuestion()
65 #test runs
67 quiz creator (menuSelection())
68 #choice returned from menuSelection function passed to quizCreator function
69 print(score)
- The
quizCreator
function (line 58) takes an integer value from themenuSelection
function - In line 60, selection is used to determine the level of difficulty chosen by the user
- Lines 61-62 use a count-crontrolled loopA loop that uses a counter to track how many times the algorithm iterates. Because it is known in advance how many loops are needed it operates a specified number of times. to call the
addQuestion
function five times - Lines 63-64 use the same technique to call the
subQuestion
function - In line 67 the
menuSelection
function is passed as a parameter to thequizCreator
function
Developmental testing
Please select the difficulty level from the menu below:
1 - Easy
2 - Medium
3 - Hard
1
Level chosen = 1
What is 8 + 8?: 16
Correct!
What is 6 + 6?: 12
Correct!
What is 2 + 2?: 3
Incorrect.
What is 4 + 6?: 10
Correct!
What is 12 + 1?: 13
Correct!
What is 10 - 12?: -2
Correct!
What is 11 - 7?: 88
Incorrect.
What is 6 - 3?: 3
Correct!
What is 9 - 4?: -5
Incorrect.
What is 9 - 3?: 6
Correct!
C# implementation
Before we can proceed, we need to have both addition and subtraction options, so we modify the addition method by replacing two + symbols with two - symbols:
static void subQuestion()
{
Random ran1 = new Random();
int num1 = ran1.Next(0, 10);
Random randT = new Random(DateTime.Now.Millisecond);
int num2 = randT.Next(0, 10);
Console.Write("What is " + num1 + "-" + num2 + " ?: ");
int answer = Convert.ToInt32(Console.ReadLine());
if (answer == num1 - num2)
{
Console.WriteLine("Correct!");
Console.WriteLine("Score +1");
score = score + 1;
}
else
{
Console.WriteLine("Incorrect.");
}
}
Next, we set up a quizCreator()
method to accept the difficulty level and generate the correct number of addition and subtraction problems.
static void quizCreator(int diff)
{
// Level 1: 5 addition and 5 subtraction questions
if(diff == 1)
{
// Addition questions
for(int i=0; i<5; i++)
{
addQuestion();
}
// Subtraction questions
for (int j = 0; j<5; j++)
{
subQuestion();
}
}
Finally, we introduce a call to this function at the bottom of the main method.
static void Main(string[] args)
{
bool valid = false; // assume the name is invalid
String name = "";
while (!valid)
{
Console.Write("Enter a name: ");
name = Console.ReadLine(); // read in a name
valid = validateName(name); // validate it
}
Console.WriteLine("*****DIFFICULTY*****");
int level = showTheMenu();
Console.WriteLine("Level chosen = " + level);
quizCreator(level);
saveScore(name, score);
}
Output from a complete run-through using level 1, and a mixture of correct and incorrect answers:
Enter a name: Louise
Louise >> Accepted
*****DIFFICULTY*****
Please select the difficulty level from the menu below:
1 - Easy
2 - Medium
3 - Hard
1
Level chosen = 1
What is 7 + 4?: 11
Correct!
Score +1
What is 6 + 2?: 99
Incorrect.
What is 4 + 2?: 6
Correct!
Score +1
What is 2 + 1?: 3
Correct!
Score +1
What is 7 + 3?: 1
Incorrect.
What is 0 - 4?: -4
Correct!
Score +1
What is 3 - 7?: 37
Incorrect.
What is 4 - 4?: 0
Correct!
Score +1
What is 3 - 2?: 1
Correct!
Score +1
What is 0 - 9?: -9
Correct!
Score +1
Press any key to continue . . .