Higher – Procedures – Horse Hands

A computer program stores the names, ages and height (the height of horses is measured in ‘hands’ – for example, 16) of fifteen horses in a riding school. The user of the program will be asked to select a horse by entering a maximum age and height of the horse they wish to ride. The data for the fifteen horses will be used to provide the user with a list of suitable. A horse is suitable if its age and height are both less than or equal to the values entered by the user.

To solve the program above we first of all have to break the program into smaller steps.

  1. A computer program stores the names, ages and height (the height of horses is measured in ‘hands’ – for example, 16) of fifteen horses in a riding school.
  2. The user of the program will be asked to select a horse by entering a maximum age and height of the horse they wish to ride.
  3. The data for the fifteen horses will be used to provide the user with a list of suitable. A horse is suitable if its age and height are both less than or equal to the values entered by the user.

We then have to look at how data is passed inside the program

This allows us to create a program that uses procedures to solve the problem.

def storeHorses():
    names=["Bob","Frank","Sue"]
    ages=[4,5,6]
    heights=[12,13,14]
    return names,ages,heights

def getSearch():
    maxAge=int(input("What is the max age you want to ride > "))
    maxHeight=int(input("What is the max height you want to ride > "))
    return maxAge,maxHeight

def displayHorses(names,ages,heights,maxAge,maxHeight):
    for horse in range(3):
        if ages[horse]<=maxAge and heights[horse]<=maxHeight:
            print(names[horse],ages[horse],heights[horse])

def main():
    #set up up variables
    names=[]
    ages=[]
    heights=[]
    maxAge=0
    maxHeight=0
    
    names,ages,heights=storeHorses()
    maxAge,maxHeight=getSearch()
    displayHorses(names,ages,heights,maxAge,maxHeight)

main()