Download Page | 1 - About project

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Page |1
Page |2
Fundamentals of Programming
(Teacher Pack)
Table of Contents
Table of Contents------------------------------------------------------------------------------------------------------------ 2
Objective of Course --------------------------------------------------------------------------------------------------------- 4
Course Type ------------------------------------------------------------------------------------------------------------------- 4
Length of Course------------------------------------------------------------------------------------------------------------- 4
Resources required --------------------------------------------------------------------------------------------------------- 4
1.
Lesson Plan ------------------------------------------------------------------------------------------------------------- 5
Lesson Number: 1 of 3 ----------------------------------------------------------------------------------------------- 5
Lesson Number: 2 of 3 ----------------------------------------------------------------------------------------------- 6
Lesson Number: 3 of 3 ----------------------------------------------------------------------------------------------- 7
2.
Topic Introduction ---------------------------------------------------------------------------------------------------- 7
What is programming all about? ---------------------------------------------------------------------------------- 8
There are lots of Programming Languages. Let’s look at the most popular ones…------------------- 8
C, C++, C# and Objective C ------------------------------------------------------------------------------------------ 8
Java ----------------------------------------------------------------------------------------------------------------------- 9
JavaScript -------------------------------------------------------------------------------------------------------------- 10
PHP ---------------------------------------------------------------------------------------------------------------------- 10
Python ------------------------------------------------------------------------------------------------------------------ 10
SQL ---------------------------------------------------------------------------------------------------------------------- 10
Swift --------------------------------------------------------------------------------------------------------------------- 11
So which one should we learn first? Let's look at recent statistics -------------------------------------- 11
Let’s learn Python as it is the most popular language to learn first. ------------------------------------ 12
Important things to know about Python ----------------------------------------------------------------------- 12
We will install Python V3.4.4 -------------------------------------------------------------------------------------- 12
The Python IDLE interface ----------------------------------------------------------------------------------------- 12
The process of opening IDLE is different on each operating system ------------------------------------ 13
3.
Practical Application of the Technology ------------------------------------------------------------------------ 14
Task 1: Basic arithmetic operators------------------------------------------------------------------------------- 14
Python Basics - Values, expressions, variables/ statements ---------------------------------------------- 14
Task 2: Creating variables ------------------------------------------------------------------------------------------ 15
Task 3: What if you forget the value of a variable?---------------------------------------------------------- 15
Page |3
Task 4: What if you want to change the value of a variable? --------------------------------------------- 16
Open the IDLE interface and type in the following: --------------------------------------------------------- 16
Tips for creating Variable names! -------------------------------------------------------------------------------- 16
Watching out for errors -------------------------------------------------------------------------------------------- 16
Task 5: Name Error Example -------------------------------------------------------------------------------------- 16
Task 6: Name Error Example two -------------------------------------------------------------------------------- 17
Task 7: Syntax Error ------------------------------------------------------------------------------------------------- 17
The IDLE file editor -------------------------------------------------------------------------------------------------- 17
Task 8: A basic programme ---------------------------------------------------------------------------------------- 18
How did we do that? Let's examine it line by line... --------------------------------------------------------- 19
Functions --------------------------------------------------------------------------------------------------------------- 19
Task 9: Using the rand(int) function ----------------------------------------------------------------------------- 19
Boolean Data Types, Comparison Operators and Conditions --------------------------------------------- 20
Task 10: Comparison operators and conditions result in a Boolean value ---------------------------- 20
Task 11: What if the two values are the same? -------------------------------------------------------------- 21
Task 12: What if the two values are the same? (Take Two) ----------------------------------------------- 21
Now try building more expressions in the IDLE using the Comparison operators in this table: -- 21
Code Blocks------------------------------------------------------------------------------------------------------------ 21
Task 13: Practice making blocks of code ----------------------------------------------------------------------- 22
4.
Student Engagement with Topic --------------------------------------------------------------------------------- 23
Now we have practised the fundamentals of Python, we will programme a small game --------- 23
How did we do that? Let's examine it line by line... --------------------------------------------------------- 24
Class Exercise --------------------------------------------------------------------------------------------------------- 26
5.
Industry Exposure---------------------------------------------------------------------------------------------------- 27
Prepare for a career in Networking – Things you can do straight away! ------------------------------- 27
Delve in and see if you like it! ------------------------------------------------------------------------------------- 28
What do the experts say…? --------------------------------------------------------------------------------------- 28
6.
Course Conclusion --------------------------------------------------------------------------------------------------- 29
7.
Appendix I - References -------------------------------------------------------------------------------------------- 30
8.
Appendix II – Help with installing Python ---------------------------------------------------------------------- 31
Page |4
Objective of Course
This course introduces the fundamental building blocks of programming and teaches you how to
write basic programmes using the Python language.
Course Type
Elective
Length of Course
It is envisioned that this course should take three classes to complete.
Resources required




Internet access is required to run the student presentation. (Produced using Microsoft Sway).
Projector and screen are required for display.
Good quality audio/video is required to listen to/watch the embedded YouTube videos.
Python Version 3.4 is required for this course. Please download onto teacher and student
computers. Instructions are available here.
Page |5
1. Lesson Plan
Lesson Number: 1 of 3
Lesson Objective: To cover the following:
What is programming?
Introduction to the most popular programming languages
Overview of current statistics on most important languages to learn
Overview of the Python Installation process
Python fundamentals
Lesson Content: All material provided in ‘Taster Course’ teacher pack.
Method of Delivery: Sway presentation https://sway.com/qGxSz2jUXtIk3TLT
Python Demonstrations
In-class Activity:
Watch all embedded YouTube videos
Task 1: Basic arithmetic operators
Equipment Required: PC or laptop, projector, audio/video display plugins
Projected Duration: One 45-minute class
Learning Outcomes: Overview of Computer Programming and introduction to a programming
interface i.e. The Python IDLE environment.
Page |6
Lesson Number: 2 of 3
Lesson Objective: To learn about the following:
Values, expressions and variables
Programming errors
Running a programme
Functions
Boolean data types, comparison operators and conditions
Code blocks
Lesson Content: All material provided in ‘Taster Course’ teacher pack.
Method of Delivery: Sway presentation: https://sway.com/qGxSz2jUXtIk3TLT
Python Demonstrations
In-class Activity:
Complete Task 2 – 13
Equipment Required: PC or laptop, projector, audio/video display plugins
Projected Duration: One 45-minute class
Learning Outcomes: Learn to Programme using Python. Understand the difference between the
IDLE window and running a programme with the File Editor.
Page |7
Lesson Number: 3 of 3
Lesson Objective: To cover the following:
Programme a small game
Understand and manipulate the programme code
Investigate careers in programming
Investigate things students can do themselves to prepare for a career in programming
Listen to what the experts have to say
Lesson Content: All material provided in ‘Taster Course’ teacher pack.
Method of Delivery: Sway presentation https://sway.com/qGxSz2jUXtIk3TLT
Python Demonstrations
In-class Activity:
Create and manipulate a game in Python
Watch all embedded YouTube videos
Equipment Required: PC or laptop, projector, audio/video display plugins
Projected Duration: One 45-minute class
Learning Outcomes: Create and manipulate a game in Python. Understand what skills are
required for a career in programming. Investigate careers in programming. Listen to what the
experts have to say.
2. Topic Introduction
Page |8
Open the Fundamentals of Computer Programming presentation and display on
projector screen: https://sway.com/qGxSz2jUXtIk3TLT. Navigate through the SWAY.
What is programming all about?
Computers are fantastic machines, and learning to programme them is not as hard as you might
think! But they cannot work alone - you must give them instructions on what you want them to do...
A computer programme is a set of step by step instructions that the computer can understand. Think
about how you build a jigsaw, it is made up of many pieces and there is a unique and specific
location for each piece.
A jigsaw is made up of many pieces and there is a unique and specific location for
each piece...
To instruct a computer, you write a programme in a language the computer
understands. So you need to learn a computer language. Similar to how you would communicate
with someone from France - you need to learn their language in order to communicate with them.
Unless you give computers the exact instructions, they will get confused and make mistakes. Telling
a computer what you want it to do is sometimes hard because you have to explain things very
logically and carefully.
All programming instructions have to be exactly correct. Computers are very picky! If it does not like
your instructions, a computer will throw back problems called bugs. But don't worry - even the best
programmers create lots of bugs.
Watch embedded video: Why are programming errors called bugs? (1:13m)
There are lots of Programming Languages. Let’s look at the most popular ones…
C, C++, C# and Objective C
The languages C, C++, Objective C and C# (pronounced "C-Sharp") all sound as if they should all form
part of the same programming family - well some do and do not...
C
Page |9



In 1972, the programming language "C" was developed by a computer scientist called Dennis
Ritchie.
C quickly became very popular and has become the most used programming language ever
made. The major reason for this, is that programmes written in C can run on all sorts of different
operating systems (Cross-platform).
C is used when you need to code something that runs quickly and does not use too much
memory. For example: the code that lets your keyboard or monitor talk to the rest of your
computer.
C++
 In the early 1980's, a Danish programmer called Bjarne Stroustrup developed "C++", also at Bell
Laboratories.
 ++ is shorthand for adding 1 to a number in programming, so C++ means “one better than C.”
 This was birth of what is called object oriented programming (OOP). The basic idea of object
oriented programming is that all of your code is arranged into logical bundles of data and
actions, instead of being spread out in a confusing way!
 The purpose of C++ is to make writing programmes easier for the programmer.
Objective C
 This was created by Brad Cox and Tom Love in 1983 but has become popular in recent years as it
is used on the iPhone, iPod Touch and the iPad.
 Objective-C is the main programming language used when writing software for Apple's
Operating Systems.
 However, Objective-C was not invented by, nor is it exclusively owned by, Apple.
 It is an open standard and has also been used by Microsoft in the past.
C# (pronounced "C-Sharp")
 C# was created by Microsoft in 2001 and is an object-oriented programming language that aims
to combine the computing power of C++ but with easier programming methods.
 Since it was produced by Microsoft it powers a lot of Windows programmes also, but it is also
used in web development.
Watch embedded video: Why C# is called C Sharp (2:20m)
Java





Java is considered the perfect language for programmers to learn due to its simplicity and
readability.
It can be used for mobile-based apps and for creating desktop applications.
It is one of the most widely used programming languages.
Java has long-term compatibility, which makes sure older applications continue to work now and
into the future.
Java is used to power company websites like LinkedIn, Netflix and Amazon.
Watch embedded video: What is Java? (1:13m)
P a g e | 10
JavaScript




JavaScript – do not confuse it with Java – is another one of the world’s most popular and
powerful programming languages, and is used to spice up web pages by making them
interactive.
It is the scripting language of the Web and is built into all major web browsers including Internet
Explorer, FireFox and Safari.
JavaScript can be used to add effects to web pages, display pop-up messages or to create games
with basic functionality.
It is also used to include animations on the web pages.
Watch embedded video: JavaScript: Interactive Web (1:36m)
PHP



Created by Danish-Canadian programmer Rasmus Lerdorf in 1994, PHP is a very popular
language since it is easy to use by people new to programming, but it also offers a lot of
advanced features for the more experienced programmer.
It is a well-known programming language and is a must for web developers.
PHP is the actual foundation of many content management systems (backend of a website),
most significantly WordPress.
Watch embedded video: PHP tutorial part 1 (2:32m)
Python





Python is a powerful language that has quickly become the most popular language in education.
It has recently knocked Java out of the top spot as the language of choice in introductory
programming courses.
It uses simple syntax, is easy-to-read since it closely resembles the English language.
It’s a great language for beginners, all the way up to professional programmers.
Python is also used by many large companies such as Google, and powers sites such as Youtube
and Reddit.
Python was named after the Monty Python films!
Watch embedded video: What is Python? (1:51m)
SQL



SQL (pronounced ‘sequel’) is the language that ranks highest in terms of job opportunity, not
only in Europe but also across the world. So having SQL knowledge makes you very employable!
Database technologies such as SQL power big and small businesses, hospitals, banks and
universities.
Every computer and person with access to technology eventually touches something SQL. For
instance, all Android phones and iPhones have access to a SQL database called SQLite and many
mobile apps developed by Google, Skype and DropBox use it directly.
Watch embedded video: What is SQL? (5:12m)
P a g e | 11
Swift



In 2014, Apple decided to invent their own programming language. The result was Swift – a new
programming language for iOS and OS X developers to create their apps.
Swift is known to be similar to C++ and Objective-C.
Companies including American Airlines and LinkedIn have been adopting Swift, and we should
see this language rise in popularity in the coming years.
Watch embedded video: Objective-C vs Swift (3m)
So which one should we learn first? Let's look at recent statistics
Discuss chart: Most popular language of choice in introductory programming courses.
(Swift, 2015)
Discuss chart: Most popular language in
terms of jobs. (The 9 Most In-Demand
Programming Languages of 2016, 2016)
P a g e | 12
Let’s learn Python as it is the most popular language to learn first.
Python was created by a programmer called Guido van Rossum in the Netherlands around 1991. The
website for all Python resources is http://www.python.org.
Important things to know about Python






It is an easy language - the coding is easy to read and understand.
Python is free. It is cross-platform and runs on all computers.
You can code in Python quicker than other languages.
Python is object-oriented.
Python files should have the extension .py.
Python has had 3 versions so far. Python 1 the initial version was quickly replaced by Version 2 in
2000, which had many new features. Python 3 - a major upgrade - was released in 2008. It is
very important to note that Version 3 was not backward compatible! (This means that Python 2
programmes will not run in Python 3).
We will install Python V3.4.4
To be able to write code using the Python language, we will need to install software called the
Python Interpreter.
Watch embedded video: Installing Python (5:21m)
Visit the following webpage for step-by-step instructions on installing Python: Click here>>
(Hendrikx, 2014) or see Appendix II
The Python IDLE interface
IDLE stands for Interactive DeveLopment Environment. The development environment is like a
word document or a notepad for writing Python programmes. In the industry, it is also referred to as
the Interactive Shell.
P a g e | 13
The process of opening IDLE is different on each operating system
On Windows



Click on the Windows (Start) button in bottom left corner
Type IDLE
Double click on IDLE to open the interface
On Mac OS X



Open Finder and click on Applications
Double click on Python 3.4 to open the folder
Then double click on IDLE to open the interface
P a g e | 14
3. Practical Application of the Technology
Code lessons have been adapted from Invent Your Own Computer Games with Python By Al
Sweigart (Creative Commons). (Sweigart, 2010)
Students who enjoy the coding examples completed in this course can download further
material from this website: Click here>>
Task 1: Basic arithmetic operators
Open the IDLE interface and use basic arithmetic operators to run expressions (sums)
Sums can be completed in the IDLE interface (similar to a calculator).
Type the following examples into IDLE.
Hit return or enter to see the result.
Python Basics - Values, expressions, variables/ statements
What are values?
In programming, values are Integers (whole numbers) such as 1, 2, 3, 4.
They are also floats or floating numbers (fractions) such as 1.0, 1.2, 1.3,
1.4 and 1.5. Whether an integer or float, they are referred to as values.
What are expressions?
The sums we added into IDLE are called expressions. Expressions are
made up of values connected by operators (arithmetic operators). When
a computer solves the expression 7 + 9 and gets the value 16, it has
evaluated the expression (worked it out). Evaluating an expression
reduces it to a single value, just like solving a maths problem reduces it a
single number: the answer.
Expressions can be of any length, but will always conclude with a single
P a g e | 15
value. For example, the expression 3 + 3 / 9 - 6 will result in 2.0 but IDLE will not show the steps. It
will only give the single value or the answer!
What are variables / statements?
You can save the value an expression evaluates to so you can use it later by storing them in
variables. Think of variables like an item which holds value (eyes = 2; nose = 1, pages = 100 and so
on). Type the name for the variable, followed by the = key (called the assignment operator), and
then the value to store in the variable. When you press ENTER you will not see anything in response.
Python will store this statement for use later.
Task 2: Creating variables
Open the IDLE interface and type in the following:
Type
x=5 (return)
y=6 (return)
x+y
Hit return or enter to see the result.
What is x + y? The variable “x” has the value of 5. The variable "y" has the value of 6 so therefore 5 +
6 = 11. The first time a variable is used in a statement, Python will create that variable.
Task 3: What if you forget the value of a variable?
In case you forget what value you assigned to a variable, you can check it by typing the variable
name into the IDLE and pressing enter or return. Its value will appear.
Type x (return)
Type y (return)
Here is the result:
P a g e | 16
Task 4: What if you want to change the value of a variable?
To change the value of a variable, all you have to do is to enter a different statement.
Open the IDLE interface and type in the following:
Type x (return)
Type x=15 (return)
Type x+y (return)
Here is the result:
We told Python that the value of "x" is no longer 5 but now 15. The expression "x + y" no longer adds
up to 11 but to 21.
Tips for creating Variable names!



Giving variables descriptive names makes it easier.
Variable names are case-sensitive. Case-sensitive means the same variable name in a different
case is considered a different variable e.g. box, BOX, Box, and bOX are four different variables.
Always use lowercase names just to be sure. It also makes your code more readable.
Watching out for errors
All programmers will make simple mistakes which will cause errors in code. Here are two basic
errors which you may come across:
1) Name Errors
Name errors will occur if you try to use a variable but have forgotten to assign a value to it.
Mistyping a variable name also causes these.
Task 5: Name Error Example
Open the IDLE interface and type in the following:
Type x+w (return)
Here is the result:
A name error results as we have not assigned a value to a variable called "w".
P a g e | 17
Task 6: Name Error Example two
Open the IDLE interface and type in the following:
Type apples = 10 (return)
Type oranges = 12 (return)
Type bananas = 11 (return)
Type aples+bananas (return)
Here is the result:
2) Syntax Errors
A syntax error will occur if you have made a mistake in writing your expression. Remember that
expressions have values connected by operators and if a value for instance is missing, Python will get
confused. A Syntax error just means that Python does not understand the instruction because you
typed it incorrectly.
Task 7: Syntax Error
Open the IDLE interface and type in the following:
Type apples + (return)
Here is the result:
A Syntax error will occur if you have made a mistake in writing your expression.
The IDLE file editor
Until now, we typed code into IDLE or the interactive shell. However, when you write programmes,
you need to be able to type in a lot of code and run it all at once. For this reason, we need to open
and use the File Editor Window.





With the IDLE window open, click on the File menu at the top of the window.
Then select New File.
A blank window will appear
This is where you will type and run your code
The two windows look similar, but just remember that the interactive shell window will have the
>>> prompt and the file editor window will not.
P a g e | 18


Name and Save your files so you can access your programmes again (Ctrl + S).
Remember that Python files will have .py extension.
Task 8: A basic programme
1. Open a new file editor window. Save the file as first.py.
2. Type the following code into the window:
#This programme welcome you to class and asks for your name.
print('Hello')
print ('Welcome to your first Python Class!')
print ('What is your name?')
myName=input()
print ('Great to meet you, ' +myName)
3. Save your programme again (Ctrl + S). It is best practice to save your code regularly.
4. To see the code functioning, click on Run > Run Module (or press F5). The programme will run in
the interactive shell.
5. Respond to the question and hit return to see the results!
P a g e | 19
How did we do that? Let's examine it line by line...
Line 1
# This programme welcomes you to class and asks for your name says hello.
This is a comment. Any text following a # sign is a comment. Comments are the programmer’s notes
about what the code does. It is common practice to place comments at the start of code (like a title).
Comments do not display when programmes are executed or run.
Lines 2, 3 and 4
print('Hello')
print('Welcome to your first Python Class!')
print('What is your name?')
In these lines of the code, we are using what is called a print() function. A function calls an
instruction that tells Python to run the code inside a function. The print() function takes the string
(collection of words) you type between the round brackets as input and displays the text on screen.
Line 5
myName = input()
This is a statement with a variable called "Name" and the input() function. When input() is called, the
programme waits for the user to enter text. The text string that the user enters becomes the value
that the function call evaluates to.
Line 6
print('Great to meet you, ' + Name)
The last line is another print() function. The expression 'Great to meet you, ' +myName in between
the parentheses of print().
Functions
So far, we have seen the print() and input() functions at work. But there are other inbuilt functions
available in Python including the rand(int) function.
The rand(int) function calls up a random integer between and including the two values you state.
The difference between the rand(int) function and the others we have seen is that the import
random statement is always used before the rand(int) function.
This informs Python that the rand(int) function is located in the random module. While Python
includes many built-in functions, some functions exist in separate programmes called modules. You
can use these functions by importing their modules into your programmes with an import
statement. Let's test it out...
Task 9: Using the rand(int) function
Open the IDLE interface and type in the following:
Type import random (return)
P a g e | 20
Type random.randint (1,10) (return)
Here is the result:
This function returns a random number from the sequence 1 to 10.
Boolean Data Types, Comparison Operators and Conditions
The Boolean Data Type
The Boolean data type has only two values: True or False. These values must be typed with a capital
"T" and "F". The rest of the value’s name must be in lowercase. You will use Boolean values (called
bools for short) with comparison operators to form conditions.
Comparison Operators
Comparison operators compare two values and evaluate to a True or False Boolean value. You’ve
already tested out the +, -, *, and / math operators. Like any operator, the comparison operators
combine with values to form expressions.
Conditions
A condition is an expression that combines two values with a comparison operator (such as < or >)
and evaluates to a Boolean value. A condition is just another name for an expression that evaluates
to True or False.
Task 10: Comparison operators and conditions result in a Boolean value
Open the IDLE interface and type in the following:
0 > 5 (return)
0 < 5 (return)
Here is the result:
The condition 0 > 5 returns the Boolean value False because the number 0 is less than the number 5.
The condition 0 < 5 returns the Boolean value True because the number 0 is less than the number 5.
P a g e | 21
Task 11: What if the two values are the same?
Open the IDLE interface and type in the following:
5 > 5 (return)
Here is the result:
The "=" Sign
Normally in maths, we use the equal sign (=) to see if two values are equal, but in Python, it is used
to store values in variables i.e. eyes = 2. In Python, use the comparison operator (==) to compare two
values. Now try again:
Task 12: What if the two values are the same? (Take Two)
Open the IDLE interface and type in the following:
5==5 (return)
Here is the result:
Now try building more expressions in the IDLE using the Comparison operators in this table:
Code Blocks
In Python, the code is organised by using indentation or code
blocks which make large pieces of code easier to read and
understand. This is a requirement of the language. You can tell
where a block begins and ends by looking at the number of
spaces at the front of the lines. A block begins when a line’s
indentation increases (usually by four spaces). Any following
line also indented by four spaces is part of the block. The block
P a g e | 22
ends when there’s a line of code with the same indentation before the block started. This means
blocks can exist within other blocks. Use the colon (:) to let Python know you want to register a new
block of code.
Task 13: Practice making blocks of code
1. Open a new file editor window. Save the file as second.py.
2. Type the following code into the window:
#This programme will ask you a quiz question.
print(‘What is the Capital of France?’)
answer=input()
if answer==’Paris’:
print(‘Correct’)
else:
print(‘That is the wrong answer!’)
3. Save your programme again (Ctrl + S).
4. To see the code functioning, click on Run > Run Module (or press F5). The programme will run in
the interactive shell.
5. Respond to the question and hit return to see the results!
P a g e | 23
4. Student Engagement with Topic
Now we have practised the fundamentals of Python, we will programme a small game
Advice for Teachers
The following piece of code is long and quite complicated. Why not email this code to your students
(copy and paste into a blank email) so they can get on with manipulating the code rather than lose
valuable class time entering data.
Try to guess a number between 1 and 20 in 5 attempts...
1. Open a new file editor window. Save the file as game.py.
2. Type the following code into the window (or copy/paste):
# Guess a randomly generated number in 5 attempts.
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Hi there, ' + myName + ', I have chosen a number between 1 & 20')
while guessesTaken < 5:
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Too low.')
if guess > number:
print('Too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Great, ' + myName + '! You guessed the number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Out of attempts. The number I was thinking of was ' + number)
3. Save your programme again (Ctrl + S).
4. To see the code functioning, click on Run > Run Module (or press F5). The programme will run in
the interactive shell.
5. Respond to the question and hit return to see the results!
P a g e | 24
How did we do that? Let's examine it line by line...
Line 1
# Guess a randomly generated number in 5 attempts.
The first line is a comment. Python will ignore everything after the # sign.
Line 2
import random
This imports the module named random so that the programme can call random.randint(). This
function will come up with a random number for the user to guess.
Line 3
guessesTaken = 0
This line creates a new variable named guessesTaken. This will store the number of guesses the
player has made. It is set at 0 as the player has not made any guesses yet.
Lines 4 – 5
print('Hello! What is your name?')
myName = input()
The first part calls the print() function. The second part lets the user type in their name and stores it
in the myName variable.
Lines 6 – 7
number = random.randint(1, 20)
print('Hi there, ' + myName + ', I am thinking of a number between 1 & 20.')
Next we call a function named randint() which will return a random integer between (and including)
the two integer arguments you pass to it. The next line calls the player by their name and tells them
that the programme is thinking of a random number between 1 – 20. The plus signs concatenate the
three strings to evaluate down to one string.
Lines 8 – 18
while guessesTaken < 6:
print('Take a guess.')
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.')
if guess > number:
print('Your guess is too high.')
if guess == number:
break
In order to tell the programme what do with the 5 guesses a while statement and a Loop are used.
Loops execute the code over and over again (up to 5 times/guesses). A while statement always has a
: colon after the condition and Python expects a new block on the next line. The expression that
follows the while keyword (the guessesTaken < 6 part) contains two values (the value in the variable
P a g e | 25
guessesTaken, and the integer value 6) connected by an operator (the <“less than” sign). The < sign
is a comparison operator. Comparison operators compare two values and evaluate to a True or False
Boolean value. If True (which it does the first time, because the value of guessesTaken is 0),
execution will enter the while-block at the second line and keep going down. However, when it
reaches the end of the while-block it will loop back up to the while statement again. Each time the
execution goes through the loop is called an iteration.
As long as the condition is True, the programme keeps executing the code inside the while-block
repeatedly until the first time the condition is False. Think of the while statement as saying, “while
this condition is true, keep executing the code in the following block”.
Once the player has taken a guess, the number of guesses should be increased by one. On the first
iteration of the loop, guessesTaken has the value of 0. Python will take this value and add 1 to it. 0 +
1 evaluates to 1, which is stored as the new value of guessesTaken.
The execution of the if statement will run the code in the block if the if statement’s condition
evaluates to True. If the condition is False, then the code in the if-block is skipped. Using if
statements, you can make the program only run certain code when you want it to. The if statement
works almost the same as a while statement, too. But unlike the while-block, the execution doesn’t
jump back to the if statement at the end of the if-block. It just continues down to the next line. In
other words, if statements don’t loop
A break statement tells the execution to jump immediately out of the while-block to the first line
after the end of the while-block. The break statement doesn’t bother rechecking the while loop’s
condition. The break statement is only found inside loops, such as in a while-block.
If the player’s guess isn’t equal to the secret number, the execution reaches the bottom of the whileblock. This means the execution will loop back to the top and recheck the condition on
guessesTaken. When guessesTaken has the number 6 stored in it, the while statement’s condition
(guessesTaken < 6) is False, since 6 isn’t less than 6. Because the while statement’s condition is False,
the execution moves to the first line after the while-block. The loop has ended.
Lines 19 – 25
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
P a g e | 26
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
The next line has no indentation, which means the while-block has ended. The execution left the
while-block either because the while statement’s condition was False (when the player runs out of
guesses) or the break statement was executed (when the player guesses the number correctly). If
statements again are used to generate whether the successful or unsuccessful response is sent to
the player.
Class Exercise
Can you manipulate the code?
Ask students to change the code in the relevant areas to guess a number between 1 and 10 in 3
attempts...
P a g e | 27
5. Industry Exposure
Prepare for a career in Programming – Things you can do straight away!
Be a good communicator
Take part in group projects at school. While programming has been
perceived as being a loner's job, the opposite is actually the case! If you
work for a company, you will be part of a development team and be
expected to communicate and work well with others. Simple practices
such as reading text out loud, publishing an article on the web or facing
critiques can help you prepare for working in a team environment.
Approach problems logically
Compare programming to putting a jigsaw together: you need to work out
how to connect one piece of your programme to another, and it is not
always easy. Break it down and make it simpler. Learn to draw out your
programmes into flowcharts. Simple things like making jigsaws and playing
sudoku will also help!
Be patient and set realistic goals
If you are looking to eventually have a programming job full-time, lots of
patience is a must because programmers are expected to spend a great deal of
time working with code and trying to solve bugs in code. Learn to tackle
problems in small pieces rather than entire chunks. Set achievable and realistic
goals for yourself!
Learn how a computer works
It will help in the future, if you have a solid understanding of the workings of a computer. Knowledge
of operating systems, network technologies and software can also help! There are lots of excellent
free video tutorials and presentations available online to help.
Watch embedded video: Inside your computer (4:12m)
Complete online courses
The internet is full of free courses in Programming languages and
techniques. Watch out for our 'Follow on Resource Pack' for help.
P a g e | 28
Delve in and see if you like it!
I want to design Animations and Graphics for games, movies and web pages
If you want to get into animation and design, you should learn major graphics programmes such as
Adobe Photoshop, Illustrator and Animate. There are free alternatives such as Gimp and Pixlr.
I want to develop Databases for large companies
To work in this area, become familiar with the database technologies being used in the industry Microsoft Access is good to start with but SQL is the most popular!
I want to be a Software Developer and create games and apps
If you want to be a software developer, gaining experience is a must – create programmes in your
free time. Learn one or more programming languages. Use open source programmes as they are a
great way to learn a language.
I want to be a Web Designer
For basic web designing positions you should have a good understanding of HTML, the Internet and
web servers. Try to develop your own page! Again use open source technologies when
experimenting here.
What do the experts say…?
Watch embedded video: What Most Schools Don't Teach (5:43m)
P a g e | 29
6. Course Conclusion
Thank you for completing the Fundamentals of Computer Programming Taster Course and being
part of the GYIT programme.
All students who participate in the GYIT programme will also receive a Follow on Resource Pack.
This will comprise a comprehensive list of free and open source websites and tutorials so students
can continue on their programming journey.
For further information on the GYIT programme, please visit our website: Click here>>
P a g e | 30
7. Appendix I - References
Hendrikx, M. (2014, August 10). How to Install Python on Windows. Retrieved from How-to Geek:
http://www.howtogeek.com/197947/how-to-install-python-on-windows/
Sweigart, A. (2010). Invent Your Own Computer Games with Python. Albert\Sweigart.
Swift, J. (2015, February 12). Most Popular Computer Languages 2015. Retrieved from I
Programmer: http://www.i-programmer.info/news/98-languages/8277-most-popularcomputer-languages-2015.html
The 9 Most In-Demand Programming Languages of 2016. (2016, January 27). Retrieved from Coding
Dojo Blog: http://www.codingdojo.com/blog/9-most-in-demand-programming-languagesof-2016/
P a g e | 31
8. Appendix II – Help with installing Python
Step 1: Go to https://www.python.org/downloads/
Step 2: Select download version Python 3.4.4
Step 3: Choose the x86 MSI installer
Step 4: Click on Run to install the programme.
P a g e | 32
Step 5: Click Next
Step 6: Click Next
Step 7: Python will install…
P a g e | 33
Step 8: Click on Finish to complete