3_2_11 Structured Programming

You should be able to:

  • Describe the structured approach to programming.
  • Explain the advantages of the structured approach.

REVISE:

What is structured programming?

Structured programming refers to creating blocks of code made up of:

  • Subroutines
  • If/else statements
  • Loops

What are the advantages of the structured approach?

Advantages are:

  • It can improve the quality of the code because it is structured in blocks and each block is tested independently
  • Decisions can be made through branching (if statements, while loops)
  • It stops the need for GOTO statements that can lead to confusing code
  • It can improve the clarity of the code because subroutines make it easier to see clearly what is happening at each stage
  • It can reduce development time through faster error handling and the ability to work with other programmers
  • It makes it easier to find and fix errors because they are isolated in subroutines

TEST:

  1. Download and print the test paper here: https://drive.google.com/open?id=0B5fLtQ0Xgr2PSTZTSWRaVWZMZ1k
  2. Try the mock test yourself.
  3. Use the 3.2.11 Walking Talking Mock below to guide you through answering the questions.

Programming Task

Topic 2 Programming Challenge 1.pdf

The two text files can be download here:

A Possible Solution (TRY IT FIRST BEFORE LOOKING HERE!)

The video below shows my attempt at the solution. If you decide to watch the video then look out for:

  • How I error check
  • How many mistakes I make before I get it right
  • How I used resources from other programs (snippets from computingtextbook) to help me remember some of the syntax
  • How I structured the solution
  • How I used meaningful identifiers

This solution isn't perfect and another hour or two of coding would make it an efficient solution.

A good programmer would have...

  • More annotations
  • Accompanying documentation
  • A more efficient "enterReg()" subroutine
  • The option to go back to the main menu after checking the authentication

The Python Code from the Video:

def enterReg(): #user enters the registration
    lengthCheck = ""
    first2Letters = ""
    first2region = ""
    fthLetterspace = ""
    second2year = ""
    lastThree = ""
    print ("Enter a registration number to authenticate")
    registration=input().upper()
    print ("You entered..."+registration)
    print ("Is this correct? Y / N")
    answer = input().upper()
    while answer not in ["Y","N"]:
        print ("You must enter Y or N")
        answer = input()
    if answer == "Y":
        trueCount=0
        print ("Registration Entered")
        print ("Authentication started")
        lengthCheck=(checkLength(registration,lengthCheck))
        if lengthCheck==True:
            trueCount+=1
        print ("Registration Correct Length: "+str(lengthCheck))
        first2Letters=(f2Letters(registration,first2Letters))
        if first2Letters==True:
            trueCount+=1
        print ("First two characters are letters: "+str(first2Letters))
        first2region=(f2Region(registration,first2region))
        if first2region==True:
            trueCount+=1
        print ("The region is valid: "+str(first2region))
        second2year=(s2Year(registration,second2year))
        if second2year==True:
            trueCount+=1
        print ("The year is valid: "+str(second2year))
        fthLetterspace=(fthSpace(registration,fthLetterspace))
        if fthLetterspace==True:
            trueCount+=1
        print ("The 5th Letter is a Space: "+str(fthLetterspace))
        lastThree=(lstThree(registration,lastThree))
        if lastThree==True:
            trueCount+=1
        print ("The last three characters are letters: "+str(lastThree))
        if trueCount==6:
            print ("Registration Number is Authentic")
        else:
            print ("Registration Number is Fake")
    else:
        enterReg()

def quitApp(): #quits the program
    print ("Are you sure you want to quit? Y / N")
    answer=input().upper()
    while answer not in ["Y","N"]:
        print ("You must type Y or N")
        answer=input().upper()
    if answer=="Y":
        quit()
    else:
        menu()

def checkLength(registration,lengthCheck): #checks if the reg is the length is 8 characters
    regLength = (len(registration))
    if regLength!=8:
        lengthCheck=False
    else:
        lengthCheck=True
    return lengthCheck

def f2Letters(registration,first2Letters): #checks the first two are letters
    checkLetters=[]
    firstTwo=(registration[0:2])
    for letter in firstTwo:
        alphabet=letter.isalpha()
        checkLetters.append(alphabet)
    if checkLetters[0]==True and checkLetters[1]==True:
        first2Letters = True
    else:
        first2Letters = False
    return first2Letters

def f2Region(registration,first2region): #checks the first two are in a valid region
    firstTwo=(registration[0:2])
    f = open('validregion.txt', 'r')
    first2region = False
    for line in f:
        lineFirstTwo=line[0:2]
        if lineFirstTwo==firstTwo:
            first2region=True
    return first2region

def s2Year(registration,second2year): #checks if the second two are a valid year
    secondTwo=str(registration[2:4])
    f = open('validyear.txt', 'r')
    second2year = False
    for line in f:
        lineSecondTwo=str(line[0:2])
        if lineSecondTwo==secondTwo:
            second2year=True
    return second2year
    
def fthSpace(registration,fthLetterspace): #checks if the fifth letter is a space
    fthLetter=(registration[4])
    if fthLetter == " ":
        fthLetterspace=True
    else:
        fthLetterspace=False
    return fthLetterspace

def lstThree(registration,lastThree): #checks if the last three are letters
    three = registration[5:8]
    letterCount=0
    for letter in three:
        if letter.isalpha():
            letterCount+=1
    if letterCount == 3:
        lastThree=True
    else:
        lastThree=False
    return lastThree

def menu(): #menu system

    print ("Welcome to the License Authentication Application")
    print ("-------------------------------------------------")
    print ("Choose an option:")
    print ("1. Enter a Registration")
    print ("2. Quit")
    print ("Enter an option number...")
    option=input()
    while option not in ["1","2"]:
        print ("Enter either 1 or 2")
        option=input()
    if option=="1":
        enterReg()
    elif option=="2":
        quitApp()
        
menu()

SOURCE RECOGNITION - PLEASE NOTE: The examination examples used in these walking talking mocks are samples from AQA from their non-confidential section of the public site. They also contain questions designed by TeachIT for AQA as part of the publicly available lesson materials.