We use the standard algorithm input validation to check that the user is entering a value that is expected by the program. In the example below our function is used to check that the entered integer is between a max and min value.
Input Validation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
defgetValidInt(fMin,fMax):
print("Please enter a number between",str(fMin), "and",str(fMax))
number=int(input())
whilenumber<fMin ornumber>fMax:
print("Sorry try again")
print("Please enter a number between",str(fMin), "and",str(fMax))
number=int(input())
returnnumber
defmain():
min=-100
max=100
number1=getValidInt(min,max)
number2=getValidInt(1,20)
print(number1,number2)
main()
The function follows the AREA standard algorithm, that is Ask Repeat Error Ask. so
Ask for valid number
Repeat While number not valid
Display Error
Ask for valid number
Too remember this mnemonic easier think Jonathan Ross for step 2, as in Python we would use a While.
The code above makes use of local variables as well as formal and actual parameters. Can you spot them?
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.
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.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
defstoreHorses():
names=["Bob","Frank","Sue"]
ages=[4,5,6]
heights=[12,13,14]
returnnames,ages,heights
defgetSearch():
maxAge=int(input("What is the max age you want to ride > "))
maxHeight=int(input("What is the max height you want to ride > "))
I know that some of you have struggled to get Python working at home, so let me introduce you to Thonny.
Thonny is a Python IDE for beginners, it has Python built in so no need for a seperate install. It has a lot of cool features that you may find useful. I am going to demo a few features (the following text is from the Thonny homepage http://thonny.org/ )
No-hassle variables.
Once you’re done with hello-worlds, select View → Variables and see how your programs and shell commands affect Python variables.
Variables table
Simple debugger. Just press Ctrl+F5 instead of F5 and you can run your programs step-by-step, no breakpoints needed. Press F6 for a big step and F7 for a small step. Steps follow program structure, not just code lines.
Stepping through statements
Step through expression evaluation. If you use small steps, then you can even see how Python evaluates your expressions. You can think of this light-blue box as a piece of paper where Python replaces subexpressions with their values, piece-by-piece.
It took us a little while in class today to remember how 2D arrays work, so I thought I might go over it.
A 2 dimentional (2D) array is an array of arrays. So [[1,2,3,4],[5,6,7,8],[9,10,11,12]] is a 3 item array where each item is a 4 item array. This can be written as
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
You might notice that it looks like a grid (or matrix), this is where the 2D comes in to the name. In python we can create the array and assign it.
2D Array in Python
In the code above you can see the the individual elements of the 2D array are accessed using a pair of square brackets [][]. Using the image below we can see the [1][2] is number 7, as demonstrated in the program above.
We can use a loop to display each value in the 1st column.
Using a for loop with a 2D array.
Or a nested for to display each item in each array
Our 1st Python program makes use of a number of new constructs and variables.
# – Internal Commentary, anything after the # on the same line is ignored by the translator
= (Assignment) – values are assigned to the variables using an equals sign.
input() – a string is captured from the keyboard using the input() function
int() – this function changes the datatype of the given variable to an integer (Whole number)
Expression – The expression is the right hand side of the =, this is evaluated and any calculations performed, the results are then assigned to the variable on the left hand side of the equals
print() – This function displays a string. The “,” is used to concatenate strings and add a space between them.