Data types
Your technical documentation should also demonstrate your understanding of data types and how you used them in your solution. You should consider the following data types:
- numeric
- character
- string
- Boolean
- date/time
User Requirement 2.1: A main menu will greet the user with a personalised message and ask what level they want to be tested at 鈥 Easy, Medium, Hard.
Python implementation
20 def menuSelection():
21 '''a function to present the menu to the user'''
22 print('Please select the level of difficulty from the menu Below:')
23 print('1. Easy')
24 print('2. Medium')
25 print('3. Hard')
26 choice = int(input())
27 return choice
The menuSelection function (line 20) has been developed to allow the user to input a number. By default, Python will castingA way to convert a variable from one data type to another data type. Such as string to integer. this as a stringA sequence of characters often stored as a variable in a computer program. These characters can include numbers, letters and symbols. data type, therefore the function was expanded to cast the variable to an integerA whole number - this is one data type used to define numbers in a computer program. Integers can be unsigned (represent positive numbers) or signed (represent negative or positive numbers). (line 26). The function can now be developed to return the integer which can be used later in the program.
Developmental testing
Please select the level of difficulty from the menu below:
1. Easy
2. Medium
3. Hard
1
1
Press any key to continue . . .
C# implementation
static int showTheMenu()
{
Console.WriteLine("Please select a difficulty level from the menu below:");
Console.WriteLine("1 - Easy");
Console.WriteLine("2 - Medium");
Console.WriteLine("3 - Hard");
int choice = Convert.ToInt32(Console.ReadLine());
return choice;
}
This function shows the menu and collects the user鈥檚 choice of difficulty level.
static void Main(string[] args)
{
bool valid = false; // assume the name is invalid
while (!valid)
{
Console.Write("Enter a name: ");
String name = Console.ReadLine(); // read in a name
valid = validateName(name); // validate it
}
Console.WriteLine("DIFFICULTY");
int level = showTheMenu();
Console.WriteLine("Level chosen = " + level);
}
Additional code is added to the main method to call the showTheMenu()
function.
This is the behaviour of the whole program at this stage:
Enter a name: Jude
Jude >> Accepted
DIFFICULTY
Please select the difficulty level from the menu below:
1 - Easy
2 - Medium
3 - Hard
2
Level chosen = 2
Press any key to continue . . .