Go Loopy (Part I)

While loop

Repeats the execution of a block of code if its condition is true,  the while loop is sometimes referred to as a repeating if statement because the block of code will only execute if a condition evaluates as True. Using IDLE create and run a file with the following (or download this file and run)

# Initialise what is needed by the game
done = False
score = 0
high_score = 121
# game loop, where a game is played
while not done:
    answer = input('Quit Game(y/n):')
    if 'y' == answer.lower():
        done = True
    # Game Logic, simulate by adding points to the game score
    # can input negative and positive integers
    points = int(input('Enter points won this iteration:'))
    score += points
# Display the score plus if you have the high score
if score > high_score:
    high_score = score
    print('New High Score ', high_score)
elif score == high_score:
    print('Your score equals the high_score of ', high_score)
else:
    print('Your score is ', score)

print('Good Bye')

An example of a run on the console looks like:

while_score

The while statement is on line 6 with where the block of code associated with the while state on lines 7 to 13. The while’s condition uses a ‘not’ operation. This boolean operation inverts the boolean value, i.e. inverts True to False and inverts False to True.

When the program executes line 6,  it evaluates the condition to check if we are not done. If this condition is True, i.e. when done is False, then the while’s block of code is executed.

The block of code repeats until the user inputs a ‘y’ or ‘Y’ for the input on line 7,  which causes the variable ‘done’ to be assigned to True and the while’s condition will evaluate to False after completing the while’s block of code.

When the condition evaluates to False the while’s block of code is skipped and code execution skips to line 15 and the score is displayed.

Tuple Type

Like strings tuples are a sequences of used to refer to a set of values. The main difference between tuples and strings is that instead of values been single characters for a tuple the values can be any type,  integers (int),  floats,  strings and even a tuple. As with strings the individual values can be refers using a index to there position in the sequence starting at zero.

Also the values in a tuple once created cannot be changed. Which makes tuples a good type for values that do not change, i.e. constants in a program.

In a python console type the following:

my_info = ('skelly', 'kiernan', 95)
type(my_info)
my_info[0]
my_info[2]
type(my_info[0])
type(my_info[2])
my_info[0] = 'no change'

In the console this will look like:

tuple

Defining Colours

In PyGame tuples are commonly  used to define colours in the form of RGB values.  RGB is a additive colour model based on adding light from Red, Green and Blue light to create the desired colour. The following website gives a short explanation and a color picker that displays the Red, Green and Blue values.

RED    = (255,   0,   0)
GREEN  = (  0, 255,   0)
BLUE   = (  0,   0, 255)
YELLOW = (255, 255,   0)
BLACK  = (  0,   0,   0)
WHITE  = (255, 255, 255)

For the example above we define RED ( a constant as do not expect the values in the tuple to change) in terms of it’s RGB components.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

damian mooney

bits and bytes

%d bloggers like this: