±«Óătv

Data structure

When planning and coding your solution you should take time to consider what are required. Think about how you will access, edit and update data in your program. Depending on what you want to achieve, you may have to consider the following data structures:

  • Arrays
  • Text files (reading and writing)

In this maths quiz example, the teacher wants the results saved so they can be searched and retrieved at a later date. In this case, an array will not be suitable as it is not . For this reason, a file must be created.

Python implementation

def saveScore(name, score):
    '''Function to append name and acore to results file'''
    data = str(name + '\t' + str(score))
    with open ("results.txt", "a") as file:
        file.write(data)

The function saveScore takes the name and score as , (joins) and these as a string value. The data variable is then appended to a file called results.txt.

Developmental testing

Please enter your name: John
Welcome John
Please select the level of difficulty from the menu below:
1 - Easy
2 - Medium
3 - Hard
1
Level chosen = 1
What is 3 + 8?: 11
Correct!
What is 5 + 3?: 8
Correct!
What is 1 + 5?: 6
Correct!
What is 2 + 1?: 3
Correct!
What is 1 + 1?: 2
Correct!
What is 2 - 8?: -6
Correct!
What is 11 - 7?: 4
Correct!
What is 12 - 11?: 1
Correct!
What is 8 - 8?: 0
Correct!
What is 5 - 9?: -4
Correct!
Traceback (most recent call last):
  File "mathsQuiz.py", line 75, in (module)
    saveScore(name,score)
  File "mathsQuiz.py", line 67, in saveScore
    data = str(name + '\t' + score)
TypeError: must be str, not int
Press any key to continue . . .

Note: An error has occurred. It is important your developmental testing shows any errors as you progress. Where errors do occur, show what you did to correct them. In this case, Python is trying to write the score as an integer. However, the score also includes the user's name, so it should have been correctly cast as a string. This caused an error.

Fix

66 def saveScore(name, score):
67	'''Function to append name and acore to results file'''
68	data = str(name + '\t' + str(score))
  • Line 68 shows that the argument score is now cast as a string

C# implementation

Since developing the previous code, a general score counter variable was added to keep track of the actual score, rather than the system simply outputting that +1 point has been scored.

static void saveScore(String playerName, int playerScore)
{
  String path = @"C:\Projects\ConsoleApp1\datafile.txt";
            
  // Check if the path exists
  if (!File.Exists(path))
  {
	Console.WriteLine("No such file existed; one was created.");
  }

  // Append the name and score as a comma-separated list
  using (StreamWriter sw = File.AppendText(path))
  {
	sw.WriteLine(playerName + "," + Convert.ToString(playerScore));
  }
}

The function writes the scores and the user's name to a permanent storage file called datafile.txt.

Bug

Curiously, the datafile.txt outcome is not recording the score of this user as 9/10 as we expect. Instead, 5/10 is appearing.

We have discovered a bug. The score variable has been added as a variable, but the subtraction questions are not updating the score. This was only done in the addition questions.

The source of the bug is spotted when you compare the IF blocks in the ADD and SUBTRACT methods:

IF block – ADDIF block – SUBTRACT
if(answer == num1+num2)
{
  Console.WriteLine("Correct!");
  Console.WriteLine("Score +1");
  score = score + 1;
}
else
{
  Console.WriteLine("Incorrect.");
}
if (answer == num1-num2)
{
  Console.WriteLine("Correct!");
  Console.WriteLine("Score +1");

}
else
{
  Console.WriteLine("Incorrect.");
}
IF block – ADD
if(answer == num1+num2)
{
  Console.WriteLine("Correct!");
  Console.WriteLine("Score +1");
  score = score + 1;
}
else
{
  Console.WriteLine("Incorrect.");
}
IF block – SUBTRACT
if (answer == num1-num2)
{
  Console.WriteLine("Correct!");
  Console.WriteLine("Score +1");

}
else
{
  Console.WriteLine("Incorrect.");
}

We forgot to increment the score. Score = score + 1.

Fix

We now fix this in the SUBTRACT method.

if(answer == num1-num2)
{
  Console.WriteLine("Correct!");
  Console.WriteLine("Score +1");
  score = score + 1;
}
else
{
  Console.WriteLine("Incorrect.");
}

We test again by aiming for 10 out of 10 and checking datafile.txt afterwards.

Once we have done this and a score of 10 has been returned in datafile.txt, we are sure that the bug has been eliminated.

This is an example of a . There was nothing wrong with the syntax of the code, but it was not producing the desired results.