Programming - ICT Teacher

Download Report

Transcript Programming - ICT Teacher

PROGRAMMING
In
Lesson 2
STARTER ACTIVITY
Complete the starter activity in your python folder – lesson 2
Now we will see how you got on and update your self-assessment.
OBJECTIVES
 Understand that languages have data types
 Be able to use the idle editor to save programs
 Understand if statement and indentation
OUTCOMES
 All-Level 4
Know the key data types discussed in lesson
To be able to store a user input variable with help
Write, save and run your first python program
 Most-Level 5
Be able to write a these with little help.
 Some-Level 6
Independently write these and adapt them
LETS RECAP LAST LESSON
What is a program?
A program is a sequence of instructions that specifies
how to perform a computation.
What is syntax?
Refers to the spelling and grammar of a programming
language
Variables –
Boxes with numbers (or text) in.
PROGRAMS
 We were not really writing programs last yesterday – they were
statements. Programs are a sequence of many statements.
 Lets write a program.
 Open Idle (start menu, all programs, python 3.3)
 Make sure you are in the editor.
MY FIRST REAL PORGRAM
 Enter the following code
print(“What is your name?")
name = input()
print(“Hello " + name)
 Press F5, which runs the program. It will ask you to save it first - save
the program into your lesson
2 folder call it hello
MY BETTER PROGRAM
 See if you can adapt this to ask other questions.
 Edit the program in the idle editor
 Press f5 to run (and save it)
 Write the number of other questions you asked into your self
assessment book.
DATA TYPES – SAME AS IN DATABASES
Integer are whole
numbers
 Integer
 Float
2
9.4
34
3.14
23.4
2.0
 String
“hello” “python”
 Boolean
True
False
4242
2
“4”
Float numbers have a
decimal point
Strings have
“23.2” start and end
quotation marks
Only has these 2 values
2 is NOT the same as 2.0 which is NOT the same as “2”
OUR FIRST GAME
import random
print("Hello! What is your name?")
myName = input()
Type this code into the idle editor
Save it as “random one”
Press f5 to run it
number = random.randint(1, 10)
print("Well, " + myName + ", I am thinking of a number between 1 and 10.")
print("Take a guess.")
guess = int(input())
if guess == number:
print("Good job, " + myName + "! You guessed my number")
Make sure you have 4 spaces here
LETS HAVE A LOOK AT THIS.
import random We re-using an existing piece of code to give us a random number
print("Hello! What is your name?")
myName = input()
Generates a random number between 1 and 10
number = random.randint(1, 10) and stores it in the variable number
print("Well, " + myName + ", I am thinking of a number between 1 and 10.")
print("Take a guess.")
guess = int(input()) Converts the input from text to an integer
if guess == number: The == is equal to
print("Good job, " + myName + "! You guessed my number")
THE IF STATEMENT
Starts
with if
Guess == number the condition that evaluates to either True or False
: is the end of the if statement.
if guess == number:
print("Good job, " + myName + "! You guessed my number")
The print statement is run ONLY if the condition is True.
Notice the spaces.
These are very important in python.
CONDITIONS
Operator
Operator Name
Sign
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
==
Equal to
!=
Not equal to
6>5
True
3 <= 4
False
2 == 3
False
4=5
Error, should be ==
45 != 2
True
3 == 3.0
Error – remember data types
Eggs > pizza Depends on the values of
Eggs and pizza variables.
INDENTATION – 4 SPACES
if 2 == 2:
print("Good job, ")
print (“you are good”)
print (“Are we sure”)
Which of these lines of code are run.
yes
yes
yes
if 24 == 87:
Which of these lines of code are run.
print("Good job, ")
no
print (“you are good”) no
print (“Are we sure”)
yes
CHALLENGES
 Level 4 – Change the range of the random number to 1 to 20
Hint: change this statement number = random.randint(1, 10)
 Level 5 - Display the random number if the user does not guess it
Hint you might need this part of a statement
if guess != number:
This statement converts an integer type into a string type.
number = str(number)
 Level 6 - Let the user have 2 attempts to guess the number
PLENARY
Update your self-evaluation for this lesson.
FUN TIME
Try the tangram game
Use left mouse button to drag,
right mouse button clicks to turn tiles,