0% found this document useful (0 votes)
186 views

T2 Worksheet 2

Uploaded by

arushi.agupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

T2 Worksheet 2

Uploaded by

arushi.agupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Worksheet 2 Defensive design

Unit 8 Logic and languages

Name:...................................................................................................... Class: ......................

Task 1
(a) Write an algorithm which performs the following tasks:
Asks the user to enter their age, it then calls a function called rangeCheck. This function
accepts the age, lower limit and upper limit as parameters, validates the entry, which should
be between 16 and 19, and returns either True or False depending on whether age is valid
or not.
Finally, “Age valid” or “Age invalid” is output depending on whether or not the age is valid

Def rangeCheck(Age,ll,ul):
If (age>=ll) and (age<=ul):
Return True
Else:
Return False

Age= input(“Enter Age:”)


If rangeCheck(Agex,16,19):
Print(“Age Valid”)
Else:
Print(“Age Invalid”)

(b) Another programmer wants to use the rangeCheck subroutine to ensure that a clerk has
entered a quantity (quantity) of similar items between 1 and 100, and a price (price) for
each item between 10.00 and 150.00.
Write the two statements that wil be needed to validate the quantity and the price.

rangeCheck(quantity,1,100)
rangeCheck(price,10.00,150.00)

1
Worksheet 2 Defensive design
Unit 8 Logic and languages

2
Worksheet 2 Defensive design
Unit 8 Logic and languages

(c) Amend the algorithm in (a) so that it keeps asking for an age until an age within the range is
entered.

Def rangeCheck(Age,ll,ul):
If (age>=ll) and (age<=ul):
Return True
Else:
Return False

Age= input(“Enter Age:”)


while !rangeCheck(Age,16,19):
Age= input(“Enter Age:”)
Else:
Print(“Age Valid”)
break

Task 2
Part of a password verification routine is given below.

A user is asked to enter a new password twice. The password they enter is checked to see that
it has between 8 and 15 characters. The two passwords are then verified to see that they are
the same.

If the passwords are not the same or aren’t the correct length, the user is asked to enter them
again. If the passwords are valid and verified then the output “Password accepted” is given.

validPassword = False
while validPassword == False:
password1 = input("Enter password: ")
password2 = input("Enter password again to verify: ")
if password1<8 or password1>15:
print("Password must be between 8 and 15 characters")
elseif password1 != password2:
print("Passwords don’t match")
else
validPassword=True
print(“Password Accepted”)

endif
endwhile
(a) Complete the three conditions: CONDITION 1, CONDITION 2 and CONDITION 3

3
Worksheet 2 Defensive design
Unit 8 Logic and languages

(b) Complete the two lines of code that will occur if the passwords are accepted.

4
Worksheet 2 Defensive design
Unit 8 Logic and languages

(c) Write the program in a language you are familiar with.


validPassword = False
while validPassword == False:
password1 = input("Enter password: ")
password2 = input("Enter password again to verify: ")
if password1<8 or password1>15:
print("Password must be between 8 and 15 characters")
elif password1 != password2:
print("Passwords don’t match")
else:
validPassword=True
print(“Password Accepted”)

5
Worksheet 2 Defensive design
Unit 8 Logic and languages

Task 3
The following program has been written which takes an array of numbers and then prints the
average of them.

array a[5]
a = [5,3,2,8,8]
c = 0
for i = 0 to 5
c = c + a[i]
endfor
d = c / 5
print(d)

(a) The program would be improved by the use of more appropriate variable names. Rewrite
the code to make this improvement.
array array1[5]
array1 = [5,3,2,8,8]
num = 0
for index = 0 to 5
num = num + array1[index]
endfor
finalNum = num / 5
print(finalNum)

(b) The program currently uses the value 5 in calculations and the loop. It would be more
maintainable if this wasn’t hard coded in the program. Replace 5 with a piece of code, such
as a variable, to make it more maintainable.
array array1[5]
array1 = [5,3,2,8,8]
num = 0
for index = 0 to 5
num = num + array1[index]
endfor
divisor = 5
finalNum = num / divisor
print(finalNum)

6
Worksheet 2 Defensive design
Unit 8 Logic and languages

7
Worksheet 2 Defensive design
Unit 8 Logic and languages

(c) The code written is not reusable. It would be improved and more maintainable if it used a
function. Adapt the program to make use of a reusable function.
Def average(array1):
num = 0
for index = 0 to len(array1)
num = num + array1[index]
endfor
divisor = len(array1)
finalNum = num / divisor
return finalNum

array array1[5]
array1 = [5,3,2,8,8]
finalNum = average(array1)
print(finalNum)

(d) The program would be more maintainable by making use of comments. Write suitable
comments to the program.
Def average(array1):
#function for averaging
num = 0
for index = 0 to len(array1)
#goes through the array and creates a total of the numbers
num = num + array1[index]
endfor
divisor = len(array1)
#divides by the number of numbers in the array
finalNum = num / divisor
return finalNum

array array1[5]
array1 = [5,3,2,8,8]
#array created
finalNum = average(array1)
#average gotten from function and printed
print(finalNum)

You might also like