Please complete Task 1 (p5) and Task 2 (p7) for Monday.
For Task 2 you can use any design methodology, there is no need to create the actual program.
Computing teacher and a PT at Coltness High School.
Please complete Task 1 (p5) and Task 2 (p7) for Monday.
Programmers need to make sure that the data they allow into their programs is sensible. They can use input validation to check that the data is within a range.
In Python array handling can be accomplished by using Python’s built in list comprehension. This means when we are working with arrays we need to change the way we use the for loop depending on whether we are interested in the value or the position of the value.
In the code above the first for loop is simply printing the value from the list. The second loop knows the position of each item and can therefore display that information as well.
This is a programming statement which increases by a known amount, each time it is called.
Incrementing counters are often used inside loops to keep track of the number of iterations.
In the example above the tries=tries+1 line is keeping track of how many times the loop has been iterated.
Today we investigated the use of the if statement, this is a selection statement which allows Python to make choices.
We have updated our dice roll program from last time to include a for loop and multiple if statements. The program now displays a summary of the results of 1000 dice rolls.
For homework the class were asked to TRACS the program and we will discuss how efficient it is next time.
Today the class carried out an investigation into the JPEG image compression technique. The pupils were tasked with finding out.
The Pupils had to report on a powerpoint slide and include an example of a good and bad use of JPEG.
A function is a predefined block of code which is used then programming. It often contains a number of commands and steps.
Here are some of the functions we have met
This function displays the string that is contained in it parenthesis ().
print(‘Hi there, how are you?)
This function displays the string in its parenthesis but allows the user to enter a string that is then returned and assigned to a variable.
age=input(‘What is your age?’)
Converts the contents of its parenthesis to a string
check=input(‘Is your age ‘+str(age))
Outputs the length (number of characters) of a string or the size (number of elements) in an array
print(‘The length of your name is’,len(name))
Converts a value to a floating point number
weight=float(input(‘How many KG do you weight’))
Converts a value to an integer
age=int(input(‘What is your age?’))
Python is a general purpose language which means that it can be used to solve many different tasks. However Python does not have a command for every single eventuality. So programmers have created document with lots of different functions, these are called module libraries.
The import command is used to import module libraries into python programs.
The random module stores a number of functions, to get access to the function we want we must first import random then use dot notation to specify the function we want to call. So random.randint calls the randint function from the random module library we imported previously.
There are libraries to do many things and they all should come with documentation to make them easy to use. Like all python programs they are open source so you can have a look inside to see how they work. Here are some example
The print() function displays a string to the screen. It can be used like this.
Note that the last line has quotes around the calculation causing the string to be displayed.
You can use either a single quote ‘ or double quotes ” within the brackets. More on this later.
Python input() function returns a string to the assigned variable. This can cause us problems when we want to store integers or floats.
We can use the int() function to convert the string to a integer or the float() function to convert it to a floating point. It is better to do this when you get the input or initialise the variable as you may forget later in the program.