4.
1 Logical Tests and choice
To vary what a program does you use a conditional structure. This type of structure
is called an ‘if ‘ structure. The structure begins with the word if and then had a
logical test. The commands inside the if structure are only carried out if the test is
true.
Make a python program
You can use ‘if..else’ structures in python programs.
Make a logical test
Operator What it means
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Inside the conditional structure
Now you must put commands inside the conditional structure. The computer will
carry out these commands if the test is TRUE.
if answer==”Y”: Put a colon at the end of the logical test.
All the commands that follow the colon will be indented. That means they will be
set in from the left of the file window.
if answer==”Y”:
result=number1+number2
print(result)
The computer will only carry out the indented commands if the test is TRUE.
Example 1:
number1=70
number2=80
answer=input(“do you want to add? (Y) “)
if answer==”Y”:
result=number1+number2
print(result)
Example 2:
playerOne = input('Type in your name:')
playerTwo = input('Type in the name of your opponent:')
if playerOne == playerTwo:
print('Wow! You both have the same name')
Example 3:
age = 15
if age >= 13 and age <= 19:
print('You are a teenager')
if and else in python
To do the same thing in python, you enter the word else followed by a colon.
Example 1:
if answer==”Y”:
result=number1+number2
else:
result=number1-number2
print(result)
Example 2:
hourOfDay = 13
if hourOfDay < 12:
print('Good morning')
else:
print('Good afternoon')
Activity, Extra challenge & Test (Page 93)
4.2 Add up total
Loops
Every loop has an exit condition. The exit condition is how you stop the loop.
There are two types of loop.
✓ A counter loop: (or fixed loop), repeats a set number of times then stops.
✓ A conditional loop(or condition-controlled loop), is controlled by a logical
test.
Python program with a counter loop
In python a counter loop is called a for loop. To make a loop that repeats 10 times
the command is:
For i in range(10):
Python program to add two numbers together
for i in range(10):
num1=input("enter a number")
num1=int(num1)
num2=input("enter a number")
num2=int(num2)
result=num1+num2
print(result)
The commands inside the loop are indented. The indented commands will
repeat.
Increase a variable in python
This python program sets total to 20. Then increases the value of total by 10
Example 1:
total=20
total=total+10
print(total)
Example 2:
total=20
number=input("enter a number")
number=int(number)
total=total+number
print(total)
Use loop to add up total
total=50
for i in range(5):
number=input("enter a number")
number=int(number)
total=total+number
print(total)
Activity, Extra challenge & Test (Page 97)
4.3 Conditional loop
A conditional loop is controlled by a logical test. The result of the logical test tells
the computer whether to repeat the loop or stop.
When to use a conditional loop
Use a loop when you want to repeat commands inside a program.
✓ If you know exactly how many times you want to repeat commands, use a
counter loop.
✓ If you don’t know exactly how many times you want to repeat commands,
use a conditional loop.
Conditional loop in python
Its also called a while loop. The first line of a while loop has these features:
✓ The word while
✓ A logical test
✓ A colon
The loop continues while the condition is true. If the condition is false, the loop will
stop.
What logical test
answer != 0
Remember != means not equal to
If the answer does not equal to 0, the test is True. The loop will continue.
If the answer equals 0, the test is False. The loop will stop.
Conditional loop commands looks like;
while answer !=0:
Program 1
total=0
number=input("enter a number")
number=int(number)
while number!= 0:
total=total+number
number=input("enter a number")
number=int(number)
print(total)
Activity, Extra Challenge, Test Page 101
4.4 A class project
Bird program Class discussion
Activity
Python IDE features to help you find and fix errors
✓ Colour and layout when you write the program
✓ Error messages when you run the program.
Syntax errors
The rules of a programming language are called syntax. If you break the rules you
make a syntax error.
✓ Translating: the computer turns the commands into machine code.
✓ Running: the computer executes the machine code commands.
Fixing the problems
Common syntax errors
✓ Using the wrong word.
✓ Not using indent.
✓ Leaving out the colon.
✓ Using a single equals sign.
BIRD PROGRAM
total=0
visitor=input("type Y if you see a bird")
while visitor=="Y":
visitor=("type Y if you see a bird")
total=total+1
print(total)
Activity, Extra Challenge, Test Page 105
4.5 Extend class project
Input a number
Program
total=0
visits=input("enter the number of visits")
visits=int(visits)
while visits =="Y":
visits=input("enter the number of visits")
visits=int(visits)
total=total+1
print(total)
Exit condition
Changing the exit condition. The loop will continue if the number of visits is
anything but 0. If the user enters the number 0 the loop will stop.
Program
total=0
visits=input("enter the number of visits")
visits=int(visits)
while visits != 0:
visits=input("enter the number of visits")
visits=int(visits)
total=total+1
print(total)
Activity, Extra Challenge, Test Page 109
4.6 Readable and user friendly
Some things that make a program easy to use are;
1. Simple inputs and outputs
Tips
✓ Make the input shorts and simple to enter the right inputs.
✓ Add a prompt. The prompt is the text inside the brackets.
✓ If you include a space at the end of the prompt, the interface looks better.
The program commands that handle inputs and outputs are called the interface.
Bad input: name=input(“name”)
Better version: name=input(“what is your name?”)
2. Clear outputs
Programs should end with an output.
✓ Bad output: print(total)
✓ Better version: print(“we saw this many birds” , total)
3. Other on screen messages
For example you can add a title to explain what the program does.
4. Other useful print commands
Include “\n” in a print command to make new line.
print(“\n”)
Making readable programs
Two things that make a program easy to read are:
✓ Well chosen variable names.
✓ Comments
They are messages you add to your program. In python you mark a comment
with the hash symbol #
Activity, Extra Challenge, Test Page 113