Pseudocode & Python - 14
Pseudocode & Python - 14
& PYTHON
Computer Science Edexcel (9-1)
TOPIC 5: DEVELOP CODE
OPERATORS
% MOD in Python
// DIV in Python
** Exponent in Python
VARIABLES & CONSTANTS
Variable:
It is a memory location which is used to store data during the
execution of a program. It has a fixed data type and a
meaningful name. It’s value can be changed throughout the
code.
Constant:
It is a memory location which is used to store data during the
execution of a program. It has a fixed data type and a
meaningful name. Once created and allotted a value, It’s value
cannot be changed throughout the code.
DATA TYPES
Python
int
float
bool
(str with 1
character)
str
DECLARATION(CREATION) OF VARIABLES & CONSTANTS
VARIABLE
Pseudocode Python
Syntax DataType VariableName There is no need to Declare / Create
Explanatio Creates a variable of the mentioned data type and variables in Python with a set data
n name type.
Example INTEGER age
REAL weight In python, variables are
BOOLEAN correct automatically created once they are
CHARCHTER age
CONSTANT initialized
Pseudocode Python
Syntax CONST DataType ConstantName There is no concept of a CONSTANT in
Explanatio Python
Creates a constant of the mentioned data type
n
Example CONST INTEGER age If a fixed value has to be stored, its
CONST REAL PI stored in a variable and programmer has
CONST BOOLEAN correct to remember it on his own
CONST CHARCHTER age
INITIALIZATION(STARTING) OF VARIABLES & CONSTANTS
Pseudocode Python
Syntax SET Variable TO <value> Variable = <value>
Explanatio
Assigns a value to a variable. Assigns a value to a variable.
n
Example SET Counter TO 0 Counter = 0
SET MyString TO ‘Hello world’ MyString = ‘Hello World’
Pseudocode Python
Syntax SET Variable TO <expression> Variable = <expression>
Explanatio Computes the value of an expression Computes the value of an expression and
n and assigns to a variable. assigns to a variable.
Example SET Sum TO Score + 10 Sum = Score + 10
SET Size to LENGTH(Word) Size = LENGTH(Word)
TYPE COERCION
Type Coercion means to change the data type of a variable
Pseudocode Python
Syntax SET <NewVariable> TO <NewVariable> =
<NewDataType>(<variable>) <NewDataType>(<variable>)
Explanatio Changes the data type of <variable> to
Changes the data type of <variable> to
n <NewDataType> and saves in
<NewDataType> and saves in <NewVaraible>
<NewVaraible>
Example SET Age TO 17 Age = 17
SET Name TO ‘Ali’ Name = ‘Ali’
SET NumString TO ‘546’ NumString = ‘546’
SET GPA TO‘3.4’ GPA = 3.4
Q2. Declare 3 variables to store the price, availability and name of the product and a constant to
store the discount. Initialize the price with 500, availability with YES and name of product Cola Next.
The discount offered is of 25.5 rupees. Code the same in python.
PRACTICE QUESTIONS
Q1. Declare five variables with suitable datatypes and meaningful names to store your name, age,
gender (M or F), weight, and status of your mood whether its happy or not. Code the same in python.
PSEUDOCODE
STRING Name
INTEGER Age PYTHON
REAL Weight
CHARACTER Gender Name = ‘Ubair Khan’
BOOLEAN Happy Age = 29
Weight = 63.5
SET Name TO ‘Ubair Khan’ Gender = ‘M’
SET Age TO 29 Happy = YES
SET Weight TO 63.5
SET Gender TO ‘M’
SET Happy TO YES
Q2. Declare 3 variables to store the price, availability and name of the product and a constant to
store the discount. Initialize the price with 500, availability with YES and name of product Cola Next.
The discount offered is of 25.5 rupees. Code the same in python.
PRACTICE QUESTIONS
Q2. Declare 3 variables to store the price, availability and name of the product and a constant to
store the discount. Initialize the price with 500, availability with YES and name of product Cola Next.
The discount offered is of 25.5 rupees. Code the same in python.
PSEUDOCODE
Q4. Take 3 numbers as input. Two of these numbers must be of integer data type and one of real/float
data type. Calculate their average and store the result in a variable and output the result with an
appropriate message. Code the same in python.
PRACTICE QUESTIONS
Q3. Take 2 numbers as input. Add them and store the result in a variable and output the result with
an appropriate message. Code the same in python.
PSEUDOCODE PYTHON
Q6. Write a pseudocode which takes the marks of a students as input and tells if the student has
passed the exam or not. Code the same in python.
Q7. Write a pseudocode which takes the marks of the a student and displays the grade as per the
given grade policy. Code the same in python
Grade Policy:
90+ A*
80-89 A
60-79 B
50-59 C
Below 50 F
Q8. Write a pseudocode which takes three different numbers and displays the largest number with
suitable prompts. Code the same in python.
PRACTICE QUESTIONS
Q5. Write a pseudocode that takes the age of a child as an input and if its above 12, displays that the
child is allowed to enter the ride. Code the same using python.
PSEUDOCODE PYTHON
PSEUDOCODE PYTHON
PSEUDOCODE PYTHON
Pseudocode:
Pseudocode:
1. REPEAT..TIMES..END REPEAT
1. REPEAT…UNTIL
2. FOR..FROM..TO..DO..END FOR
2. WHILE..DO..ENDWHILE
3. FOR..FROM.. TO..STEP..DO..END FOR
Python:
1. for.. in range(X): Python:
2. for.. in range(X,Y): 1. while ():
3. for.. in range(X,Y,Z):
DEFINITE LOOPS / COUNT
CONTROLLED
PSEDUOCODES LOOP
<id> : A variable created by the loop and only exists in the loop
<X>, <Y>, <Z>: Numbers OR an integer type variable OR an expression which solves to result in a number
REPEAT..TIMES..END REPEAT FOR..FROM..TO..DO..ENDFOR FOR..FROM..TO..STEP..DO..ENDFOR
FOR <id> FROM <X> TO <Y> STEP
REPEAT <X> TIMES FOR <id> FROM <X> TO <Y> DO
<Z> DO
Syntax <commands> <commands>
<commands>
END REPEAT END FOR
END FOR
Repeats the <commands> for each value of Repeats the <commands> for the values of
<id> starting from X to Y. <id> from X to Y with <id> getting
• Starts with id=X increment of Z each time.
Repeats the • Executes the commands
Explana
tion <commands> for X • Increments the value of <id> by 1 • Starts with id=X
number of times. • Executes the commands again • Executes the commands
• Repeats the process till id=Y • Increments the value of <id> by Z
• If X=1, it simply repeats commands for Y • Executes the commands again
times • Repeats the process till <id> reaches Z-1
FOR index FROM 1 TO 3 DO FOR index FROM 1 TO 10 STEP 4 DO
REPEAT 4 TIMES
Exampl SEND ‘Loop Number: ‘ TO DISPLAY SEND ‘Loop Number: ‘ TO DISPLAY
e SEND ‘Wow A Loop!’
SEND index TO DISPLAY SEND index TO DISPLAY
END REPEAT
END FOR END FOR
Loop Number: Loop Number:
Wow A Loop! 1 1
Output
on
Wow A Loop! Loop Number: Loop Number:
Screen Wow A Loop! 2 5
Wow A Loop! Loop Number: Loop Number:
2 9
DEFINITE LOOPS / COUNT
CONTROLLED PYTHON LOOP
<id> : A variable created by the loop and only exists in the loop
<X>, <Y>, <Z>: Numbers OR an integer type variable
for index in range(5): for index in range(3,7): for index in range (1,13,3):
Exampl
e print (‘Loop Number: ‘, print (‘Loop Number: ‘, print (‘Loop Number: ‘,
index) index) index)
Loop Number: 0 Loop Number: 3 Loop Number: 1
Output Loop Number: 1 Loop Number: 4 Loop Number: 4
on Loop Number: 2 Loop Number: 5 Loop Number: 7
Screen
Loop Number: 3 Loop Number: 6 Loop Number: 10
DEFINITE LOOPS / COUNT
CONTROLLED
PSEUDOCDE LOOP
VS PYTHON
Pseudocode Python
REPEAT <X> TIMES
<commands>
for <id> in range(<X>):
END REPEAT <commands>
FOR <id> FROM 1 TO <Y> DO
<commands> for <id> in range(<X>):
END FOR <commands>
(<id> not used inside loop)
FOR <id> FROM <X> TO <Y> DO
<commands>
for <id> in range(<X>,<Y>):
END FOR <commands>
FOR <id> FROM <X> TO <Y> STEP <Z> DO
<commands>
for <id> in range(<X>,<Y>,<Z>):
END FOR <commands>
DEFINITE LOOPS / COUNT
CONTROLLED
PSEUDOCDE LOOP
VS PYTHON
EXAMPLES
Pseudocode Python
REPEAT 50 TIMES
SEND ‘Hello World’ TO DISPLAY
for index in range(50):
END REPEAT print (‘Hello World’)
FOR index FROM 1 TO 50 DO
SEND ‘Hello World’ TO DISPLAY
for index in range(50):
END FOR print(‘Hello World’)
FOR index FROM 2 TO 100 DO
SEND ‘Hello World’ TO DISPLAY for index in range(2,101):
SEND Index TO DISPLAY print(‘Hello World’, index)
END FOR
FOR index FROM 0 TO 100 STEP 25 DO
SEND ‘Hello World’ TO DISPLAY for index in range(0,100,25):
SEND Index TO DISPLAY print(‘Hello World’, index)
END FOR
DEFINITE LOOPS / COUNT
CONTROLLED
PSEUDOCDE LOOP
VS PYTHON
EXAMPLES
Pseudocode Python
REPEAT 50 TIMES
SEND ‘Hello World’ TO DISPLAY
for index in range(50):
END REPEAT print (‘Hello World’)
FOR index FROM 1 TO 50 DO
SEND ‘Hello World’ TO DISPLAY
for index in range(50):
END FOR print(‘Hello World’)
FOR index FROM 2 TO 100 DO
SEND ‘Hello World’ TO DISPLAY for index in range(2,101):
SEND Index TO DISPLAY print(‘Hello World’, index)
END FOR
FOR index FROM 0 TO 100 STEP 25 DO
SEND ‘Hello World’ TO DISPLAY for index in range(0,100,25):
SEND Index TO DISPLAY print(‘Hello World’, index)
END FOR
INDEFINITE LOOPS / CONDITION
CONTROLLED LOOP
PSEDUOCODES
REPEAT…UNTIL WHILE..DO..ENDWHILE
REPEAT WHILE <condition> DO
Syntax <commands> <commands>
UNTIL <condition> END WHILE
• Repeats the <commands> WHILE the
• Repeats the <commands> UNTIL the <condition> is true & stops when <condition>
<condition> becomes True becomes false
Explanatio • It’s a Post-Condition Controlled Loop i.e. • It’s a Pre-Condition Controlled Loop i.e. condition
n condition is checked AFTER commands are is checked BEFORE commands are executed
executed
• Thus, commands might not be executed at all if
• Thus, commands will be executed at least once condition is already false
Explanation • It’s a Pre-Condition Controlled Loop i.e. condition is checked BEFORE commands
are executed
Q10. Write a pseudocode which takes 25 numbers and counts how many of them are positive and
how many are less than zero and displays the result. Your code MUST have appropriate prompts.
Code the same using python.
Q11. Write a pseudocode which takes a number as input and prints all its multiples less than 100.
Code the same using python.
Q12. Write a pseudocode which takes marks of students of a class as input and sums them. An entry
of -1 is done to indicate that the list has ended and the program then displays the total. Code the
same in python.
Q13. Write a pseudocode which takes the amounts of bills of a super store as input and continues to
add them till the total reaches Rs.100,000. It also counts how many bills were entered to reach that
total and out of those how many bills were of more than Rs.5,000. Code the same in python.
PRACTICE QUESTIONS
Q9. Write a pseudocode that takes the marks of 50 students as input and displays their average.
Code the same using python.
PSEUDOCODE PYTHON
PSEUDOCODE PYTHON
SEND ‘Enter Number’ TO DISPLAY
RECEIVE Num FROM KEYBOARD Num = int(input(‘Enter Number:’))
FOR index FROM 0 TO 100 STEP Num DO for index in range (0,100,Num):
SEND index TO DISPLAY print (index)
END FOR
PRACTICE QUESTIONS
Q12. Write a pseudocode which takes marks of students of a class as input and sums them. An entry
of -1 is done to indicate that the list has ended and the program then displays the total. Code the
same in python.
PSEUDOCODE PYTHON
SET Marks TO 0
SET Sum TO 0 Marks = 0
WHILE (Marks <> -1) DO Sum = 0
SET Sum TO Sum + Marks while (marks <>-1):
SEND ‘Enter Marks’ TO DISPLAY Sum=Sum+Marks
RECEIVE Marks FROM KEYBOARD Marks=int(input(‘Enter Marks:’))
END WHILE print (‘Sum is:’, Sum)
SEND Sum TO DISPLAY
PRACTICE QUESTIONS
Q13. Write a pseudocode which takes the amounts of bills of a super store as input and continues to
add them till the total reaches Rs.100,000. It also counts how many bills were entered to reach that
total and out of those how many bills were of more than Rs.5,000. Code the same in python.
PSEUDOCODE PYTHON
SET Greater TO 0 Greater = 0
SET Count TO 0 Count = 0
SET Sum TO 0 Sum = 0
WHILE (Sum < 100000) DO while (Sum < 100000):
SEND ‘Enter Bill’ TO DISPLAY Bill=int(input(‘Enter Bill:’))
RECEIVE Bill FROM KEYBOARD Sum= Sum+ Bill
SET Sum TO Sum + Bill if (Bill > 5000):
SET Count TO Count + 1 Greater = Greater + 1
IF (Bill > 5000) THEN Count = Count + 1
SET Greater TO Greater + 1 print (Greater, Count)
END WHILE
SEND Count TO DISPLAY
SEND Greater TO DISPLAY
FUNCTION
Pseudocode Python
Syntax import random
SET <variable> TO RANDOM(n)
<variable> = random.randint(<X>,<Y>)
Explanatio Generates a random number from X to Y-
n Generates a random number from 0 to n 1 and stores in the variable.
and stores in the variable. import random must be written to use
the built-in random function
Example SET Guess_Number TO RANDOM(50) import random
Guess_Number = random.randint(0,51)
This will generate a random number
from 0-50 and save it in variable This will generate a random number from 0-50
Guess_Number and save it in variable Guess_Number
LENGTH FUNCTION
LENGTH Function: LENGTH is a built-in function which counts the number of
characters in a string OR the number of elements in an array and stores the result in a
variable.
Pseudocode Python
Syntax SET <variable> TO LENGTH(<string>) <variable> = len(<string>)
Explanatio Counts the number of characters in the Counts the number of characters in the
n string and stores in variable. string and stores in variable.
Example SET Size1 TO LENGTH(‘Cornerstone’) Size1 = len(‘Cornerstone Is My School’)
SET Name TO ‘Imran Khan’ Name = ‘Mustanzar Hussain #$%’
SET Size2 TO LENGTH(Name) Size2 = len(Name)
Result Size1 = Size1 =
Size 2 = Size 2 =
TOPIC 6: MAKING PROGRAMS EASY TO USE
MAKING PROGRAMS EASY TO READ
TOPIC 6: STRINGS
STRINGS
INDEXING IN STRINGS
‘Computer Science’
Subject(3) =
Subject(8) =
Subject(12) =
Subject(15) =
Subject(16) =
Subject(4) =
Subject(0) =
STRINGS
STRING TRAVERSAL / SCROLLING THROUGH A STRING
Subject = ‘Computer Science’
PSEUDOCODE PYTHON
FINDING LENGTH
Length (number od characters) in a string can be found using LENGTH
Pseudocode function Python
Syntax LENGTH(<string>) len(<string>)
Explanatio Counts the number of characters in the Counts the number of characters in the
n string string
Example SET Size TO LENGTH(Subject) Size = len(Subject)
Result Size= Size=
STRINGS
STRING OPERATIONS
Subject = ‘Computer Science’
EXTRACTING A SUBSTRING
Some characters from a string can be extracted to form a new string using
SUBSTRING
Pseudocode Python
Syntax SUBSTRING(<string>, <X>,<Y>) <String>[<X>:<Y>]
Explanatio Extracts a substring from <string> Extracts a substring from <string>
n starting from character at index <X> starting from character at index <X> and
and ending at character at index <Y>-1 ending at character at index <Y>-1
Example Extracted = SUBSTRING(Subject,3,6) Extracted = Subject[3:8]
Result Extracted = Extracted =
STRINGS
STRING OPERATIONS
Subject = ‘Computer Science’
CONCATENATION OF STRINGS
Joining of two strings or a string with a character is called concatenation
Pseudocode Python
Syntax <String1> & <String2> <String1> + <String2>
Explanatio Adds <String2> after <String> and
n forms a new string Adds <String2> after <String> and
In pseudocode, A number can also be forms a new string
concatenated with a string using &
Example SET Remark TO ‘Is Nice’ Remark = ‘Is Nice’
SET New TO Subject & Remark New = Subject + Nice
SEND New TO DISPLAY print(New)
Result on
Screen
STRINGS
STRING OPERATIONS
Subject = ‘Computer Science’
Array/List: StudentNames
Index 0 1 2 3 4 5 6 7 8 9 10 11 12
Mehree
Data 'Basil Fayha Ayesha Asra Hamza Taha Areesha Noor Nadis
n
Momina Ameer Ibrahim
Array/List: StudentNames
Index 0 1 2 3 4 5 6 7 8 9 10 11 12
Mehree
Data 'Basil Fayha Ayesha Asra Hamza Taha Areesha Noor Nadis
n
Momina Ameer Ibrahim
StudentNames [0] =
StudentNames [3] =
StudentNames [11] =
StudentNames [12] =
StudentNames [13] =
StudentNames [7] =
StudentNames [2] =
StudentNames [11] =
ARRAYS/LISTS
FINDING LENGTH OF ARRAYS/LISTS
Array/List: StudentNames
Index 0 1 2 3 4 5 6 7 8 9 10 11 12
Mehree
Data 'Basil Fayha Ayesha Asra Hamza Taha Areesha Noor Nadis
n
Momina Ameer Ibrahim
FINDING LENGTH
Length (number of characters) in a array/list can be found using LENGTH
Pseudocode function Python
Syntax LENGTH(<array>) len(<list>)
Explanatio Counts the number of data elements in Counts the number of data elemeents in
n the array the list
Example SET Size TO LENGTH(StudentNames) Size = len(StudentNames)
Result Size= Size=
ARRAYS / LISTS
STRING TRAVERSAL / SCROLLING THROUGH AN ARRAY/LIST
Array/List: StudentNames
Index 0 1 2 3 4 5 6 7 8 9 10 11 12
Mehree
Data 'Basil Fayha Ayesha Asra Hamza Taha Areesha Noor Nadis
n
Momina Ameer Ibrahim
SET Subject TO [‘Basil’, ‘Fayha’, ‘Ayesha’, ‘Ayesha’, Subject = [‘Basil’, ‘Fayha’, ‘Ayesha’, ‘Ayesha’, ‘Asra’,
‘Asra’, ‘Hamza’ , ‘Taha’, ‘Areesha’ , ‘Noor’, ‘Nadis’, ‘Hamza’ , ‘Taha’, ‘Areesha’ , ‘Noor’, ‘Nadis’, ‘Maheen’,
‘Maheen’, ‘Momina’, ‘Ameer’, ‘Ibrahim’] ‘Momina’, ‘Ameer’, ‘Ibrahim’]
for index in range ( ):
FOR index FROM _____ TO _____ print StudentNames[ ]
SEND StudentNames[ ] TO DISPLAY
END FOR
ARRAYS / LISTS
STRING TRAVERSAL / SCROLLING THROUGH AN ARRAY/LIST
Array/List: StudentNames
Index 0 1 2 3 4 5 6 7 8 9 10 11 12
Mehree
Data Basil Fayha Ayesha Asra Hamza Taha Areesha Noor Nadis
n
Momina Ameer Ibrahim
FOR index 0 TO 24 DO
SEND ‘Enter Name Of Student#’ TO DISPLAY
SEND index + 1 TO DESIPLAY
RECEIVE Name[index]
SEND ‘Enter English Marks:’ TO DISPLAY
RECEIVE English[index]
SEND ‘Enter Urdu Marks:’ TO DISPLAY
RECEIVE Urdu[index]
SEND ‘Enter Maths Marks:’ TO DISPLAY
RECEIVE Maths[index]
SET Total[index] TO English[index] + Urdu[index] + Maths[index]
END FOR
FOR index 0 TO 24 DO
SEND Name[index] TO DISPLAY
SEND Total[index] TO DISPLAY
END FOR
PRACTICE QUESTIONS
Q14. Write a pseudocode which makes a result report for 5 students. The code takes the
names of the students, their English marks, Urdu marks and Maths marks one by one and
saves in separate arrays. The code then calculates the total sum across three subjects
and saves the results in a new array. At the end the total marks of each student is printed
along with the names. Code the same in python.
Names = [None]*25
English = [None]*25
Urdu = [None]*25
Maths = [None]*25
Total = [None]*25
Result: Result:
0 1 2 3
0 Fayha 0 Fayha Femal Pass CSS21
1 Taha 1D Array: e
Multiple rows and 1 Taha Male Pass CSS34
2 Basil
Only one column
3 Nadis 2 Basil Male Pass CSS44
4 Noor 3 Nadis Male Fail CSS09
5 Ayesha 2D Array: 4 Noor Femal Fail CSS22
6 Ali Multiple rows and e
multiple colunms 5 Ayesh Femal Fail CSS11
7 Sara
a e
8 Maya
6 Ali Male Pass CSS3
TWO DIMESNIONAL ARRAYS
Pseudocode Python
2D Arrays 2D Lists
Name One Unified Name for Entire 2D Array One Unified Name for Entire 2D List
All data elements in entire 2D list CAN be of
All data elements in entire 2D array are
Data Type different data type. But datatype in each
of same data type
‘column’ is preferably kept same
Indexing First element is at Index (0,0) First element is at Index [0][0]
It is essential to declare and define the As python doesn’t require declaration,
Size
size of the 2D array defining size is also not required
ARRAY <AarrayName> [<Row>,<Col>]
<ListName> = [ ] OR
SET <ArrayName> TO [[<value>,…],[.. ,
Declaratio ..],..] <ListName> = [[<value>,…],[.. , ..],..]
n&
Initializatio The first statement declares a 2D array
n called <ArrayName> and the second Initializes a two-dimensional list called
statement Initializes that 2D array with a <ListName> with OR without set of values
set of values
ARRAY 2DArray [2,3] 2DArray= [ ]
Example
SET 2DArray TO [[11,50,6],[5,4,90]] 2DArray = [[11,50,6],[5,4,90]]
2D ARRAYS/2D LISTS
LOCATING/FINDING A CHARACTER IN 2D ARRAYS/2D LISTS
0 1 2 3
Array/List: Data 0 1 2 3
0 Fayha Femal Pass CSS21 0 Fayha F 82 YES
e
1 Taha M 88 NO
1 Taha Male Pass CSS34 A 2D ARRAY
(Pseudocode 2 Basil M 90 YES
2 Basil Male Pass CSS44 )
3 Nadis M 12 NO
3 Nadis Male Fail CSS09
4 Noor F 67 NO
4 Noor Femal Fail CSS22
e 5 Ayesh F 98 NO
a
5 Ayesh Femal Fail CSS11 A 2D LIST
a e (Python) 6 Ali M 11 YES
(Consider the 2D Array on Last Slide) (Consider the 2D List on Last Slide)
FOR row 0 TO 24 DO
SEND ‘Enter Name Of Student’ TO DISPLAY
RECEIVE Name[row]
FOR col 0 TO 2
SEND ‘Enter Marks:’ TO DISPLAY
RECEIVE Marks[row][col]
END FOR
SET Total[row] TO Marks[row][0] + Marks[row][1] + Marks[row][2]
END FOR
FOR index 0 TO 24 DO
SEND Name[index] TO DISPLAY
SEND Total[index] TO DISPLAY
END FOR
PRACTICE QUESTIONS
Q16. The students of a class participated in a game and each student scored points. A 2D
list GameData has the names, genders(M/F) and points stored. Code a program which
prints the list of names of boys and girls separately, counts and prints the number of boys
and girls separately, totals the points scored by boys and girls separately and declare
who amongst the both genders won. Code in python.
Length = len(GameData)
boys = 0 print(‘Boys:’,boys)
BoysTotal = 0 print(‘Girls:,girls)
girls = 0
GirlsTotal = 0 if (GirlsTotal>BoysTotal):
print(‘Winner: Girls’)
print{‘Boys:’)
for row in range (length):
elif (BoysTotal>GirlsTotal):
for col in range(3): print(‘Winner: Boys’)
if (GameData[row][1] == ‘M’): else
print(GameData[row][0]) print(‘Tie’)
boys = boys + 1
BoysTotal = BoysTotal + GameData[row][2]
print{‘Girls:’)
for row in range (length):
for col in range(3):
if (GameDate[row][1] == ‘F’):
print(GameData[row][0])
girls = girls + 1
GirlsTotal = GirlsTotal + GameData[row][2]
RECORDS
(PSEUDOCODE FOR PYTHON 2D LISTS)
TOPIC 9: INPUT / OUTPUT
VALIDATION
Validation: The process in which the program code automatically ensures that the data
entered by the user meets a certain criteria and is reasonable.
Validation Checks: The criteria set by the coder while coding against which the entered
data is checked, are called validation checks.
1. Range Check
2. Length Check
3. Presence Check
4. Lookup Check
TIP: All types of Validation Checks are coded using WHILE LOOP in pseudocode / python
VALIDATION
Range
Check
Range Check: It is used to ensure that the data entered by user is within a specified
range.
Pseudocode Python
Example SET Age TO 0
Age = 0
WHILE (Age < 10 OR Age > 20) DO
while (Age < 15 or Age > 25):
SEND ‘Enter Age:’ TO DISPLAY
Age=int(input(‘Enter age:’))
RECEIVE Age FROM KEYBOARD
END WHILE
Explanatio This will ensure that the user enters an This will ensure that the user enters an
n age from 10 to 20. It keeps on asking for age from 15 to 25 It keeps on asking for
age unless use enters the age from age unless use enters the age from
specified range. specified range.
VALIDATION
Length
Check
Length Check: It is used to ensure that the data entered by user is of a specified
length.
Pseudocode Python
Example SET Leng TO 0
WHILE (Leng <> 8) DO Leng = 0
SEND ‘Enter Password:’ TO while (Leng != 8):
DISPLAY Password=str(input(‘Enter
RECEIVE Password FROM Password:’))
KEYBOARD Leng = len(Password)
Leng = LENGTH(Password)
END WHILE
Explanatio This will ensure that the password
n This will ensure that the password entered
entered has only 8 characters. It will
has only 8 characters. It will keep on asking
keep on asking for password if the
for password if the entered data has more
entered data has more or less
or less characters than 8
characters than 8
VALIDATION
Presence
Check
Presence Check: It is used to ensure that some form of data has been entered by user
and user hasn’t entered a blank or left the field empty
Pseudocode Python
Example SET Name TO ‘ ’
Name = ‘ ’
WHILE (Name = ‘ ’) DO
while (Name = ‘ ’):
SEND ‘Enter Name:’ TO DISPLAY
Name=str(input(‘Enter Name:’))
RECEIVE Name FROM KEYBOARD
END WHILE
Explanation This will ensure the user enters anything This will ensure the user enters anything
other than a blank space other than a blank space
VALIDATION
Lookup
Check
Lookup Check: It is used to ensure that the data entered by user is one of the
predefined acceptable data values. The acceptable data can be store pre-hand in an
array.
Pseudocode Python
Example SET Code TO ‘Nill’
Code = ‘Nill’
WHILE (Code <> ‘Alpha’ AND Code <>
while (Code !=‘Alpha’ and Code != ‘Tarvis’
Travis AND Code <> ‘Jack’) DO
and Code !=‘Jack’)
SEND ‘Enter Code:’ TO DISPLAY
Code=string(input(‘Enter Code:’))
RECEIVE Code FROM KEYBOARD
END WHILE
Explanation This will only accept Alpha, Jack or Travis
This will only accept Alpha, Jack or Travis as
as code. Every other entry will be
code. Every other entry will be rejected
rejected
TEST DATA
Test Data: The data entered by programmer to test the program to see if
its working how it is supposed to against different types of input data.
Types of Test Data:
1. Normal Test Data: These are the data values which
should be accepted by code and program should work
accordingly.
2. Boundary Test Data: If a code has Range Check
included, the last allowed values in the range are called
Boundary Values and they are entered to test if the
program accepts them and works correctly.
3. Erroneous Test Data: These are the data values which
should be rejected by the code and program should not
accept them. These can consist of any kind of garbage
TEST DATA
Example: A program is coded which takes the percentages of a student
in exams. Give examples of the three sets of test data for this code
Pseudocode Python
#Main program
rectangleLength = int(input(‘Please enter the length of the rectangle’))
rectangleWidth = int(input(‘Please enter the width of the rectangle’))
rectangleArea = rectangle(rectangleLength, rectangleWidth)
print(rectangleArea)
Grading Policy:
< 40 = D
40-60 = C
61-80 = B
81-100 = A
Your code must have all the required validation checks and prompts for the user.
3 1 3 6
2 6 12
3 18 36 36
4 72 144 144
1 3 3
2 5 5
3 8 8
4 12 12
5 17 17
6 23 23
TRACE TABLES
1 1 - 0 11 False