Category: Implementation (Computational constructs)

Incrementing Counter

This is a programming statement which increases by a known amount, each time it is called.

  • Score=Score+1
  • One=One+1
  • Bonus=Bonus+10

Incrementing counters are often used inside loops to keep track of the number of iterations.

counter

In the example above the tries=tries+1 line is keeping track of how many times the loop has been iterated.

Python Selection – if:

diceToday 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.

Python Functions

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

print()

This function displays the string that is contained in it parenthesis ().

print(‘Hi there, how are you?)

input()

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?’)

str()

Converts the contents of its parenthesis to a string

check=input(‘Is your age ‘+str(age))

len()

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))

float()

Converts a value to a floating point number

weight=float(input(‘How many KG do you weight’))

int()

Converts a value to an integer

age=int(input(‘What is your age?’))

 

Import Function

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.

dice

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

Python – print()

The print() function displays a string to the screen. It can be used like this.

  • print(“hello there”) – displays hello there
  • print(4*4) – displays 16
  • print(“4*4”) – displays 4*4

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()

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.

InputInt
The first block of code shows Python returning a string. The second block of code uses a int() to return an Integer

 

Scratch – Calculations using a loop

Today we recapped performing calculation within and using a loop. We started off redoing the 6 times table task, before improving it to display all the times tables from 1 to 10.

TimesTable

We then created a program that took in 4 ages and displayed the total only using one user created variable

Age

These two programs make use of loops to reduce the number of programming lines, this making the program more efficient.

 

Using a message box to make a choice

It is possible to use a MsgBox to make a choice in your program. You first of all assign a integer variable to the MsgBox then you change the buttons attributes to vbYesNo. vbYesNo is a VB constant that represents a value, the use of the constant means that you do not need to memories the number, vbYes and vbNo are other examples of these VB constants. An IF statement is then used to make the selection.

N5

S3 – Conditional Loops

A conditional loop is used to repeat part of the program until a condition is true.

The program shown here is a password checker. The program will exit the loop if the user guess is 1233 or the user has had 3 attempts. This uses a  complex conditional  statement with the logical operator OR, the OR logical operator returns a true value when one of the conditions are true.

The final IF statement checks if the user has entered the correct password before saying welcome.