Lesson 2 - Selection
Download
Report
Transcript Lesson 2 - Selection
Python
Lesson 2 - Selection
Python
Where do you find Python?
How do you start a new program?
Start / Computer / ICT (I:) / Python /
IDLE
In Python go to:
File / New File
How do you run a program?
What is a string?
Press the F5 key (on the top row)
A sentence. And it is usually in green
surrounded by quote marks and
brackets.
Last Lesson
You learnt about the print command
print(“Hello, my name is Mr Lyon”)
You learnt about the input function using letters/words
name = input(“What is your name? “)
print(“Hello “ + name)
You also learnt about the input function using int to use numbers
age = int(input(“How old are you? ”))
print(”I am “ + str(age) + ” years old”)
Inputting Numbers
When inputting numbers into variables you need to tell Python what data type to
expect.
We use int to tell Python we are going to enter an integer (whole number). We
use float when working with decimal places.
We use str to convert a number into a string.
Create a new program using this code and save it as addition.
Inputting Numbers
We can also use subtract (-), multiply (*) and divide (/).
num1 = int(input(“Enter the first number: ”))
num2 = int(input(“Enter the second number: ”))
total = num1 + num2
Change this sign
print (“The first number divided by the second
number is ” + str(total))Do not change this sign
Experiment using the subtract, multiply and divide symbols. Make
three programs showing you can use each.
Create new programs using this code and name it appropriately Eg.
addition, subtract, multiply and divide.
Video
Watch this short clip about coding:
Link: https://www.youtube.com/watch?v=FCMxA3m_Imc
Selection
All the programs you have been developing so far
have been sequential, this means that each
instruction is executed in a set order.
With selection the path through a program can
be decided by looking at a condition and then
taking one of a set of alternative paths based on
the result.
Selection in flowcharts is represented using the
decision symbol.
Example 1
Start
Input Age
Is
Age>=15
?
No
Yes
Display “You
can watch this
movie”
Display “You are
too young to
watch this
movie”
End
Create the program in Python. Save it as age2.
Comparison Operators
==
Is equal to
>
Is greater than
<
Is less than
!=
Is not equal to
>=
Is greater than or equal to
<=
Is less than or equal to
You can also use the logical
operators AND, OR and
NOT.
Example 2
Start
num = 4
Input num
If guess =
num
Yes
Display “Well
done”
No
Display “Try
Again”
End
Create the program in Python. Save it as num4.
Example 3
A program to guess your teachers favourite subject!
Start
favsub = “computing”
favsub = “Computing”
guess = input(“What is your ..”)
Input Subject
If guess =
favsub
Yes
Display “That is
my fav..”
No
Display “Unlucky,
try again”
End
if guess.lower() == favsub:
print(“That is..”)
else:
print(“Un..”)
Create the program. Save it as favsub.
Example 3 Solution
Example 4
Some programs may need more than two options:
day = input(“What day is it today? ”)
if day.lower() == “monday” or day.lower() == “tuesday” or
day.lower() == “wednesday”:
print(“It is the beginning of the week.”)
elif day.lower() == “thursday” or day.lower() == “friday”:
print(“It is nearly the weekend”)
else:
print(“Hurray! It is the weekend!”)
Create the program in Python. Save it as days.
Challenge 1
Use what you have learnt to try this program:
Write a program that asks the user their name and if it the same as your name
display “You’re cool!”. If it is not the same display “That’s rubbish!”
EXTENSION
Use Example 4 (favourite subject) to help you.
Add an Elif statement to allow a third option, for example use your middle
name as a second option.
Create the program in Python. Save it as elif1.
Challenge 2
Use what you have learnt to try this program:
Write a program that asks the user their favourite team and if it the same as your
team display “Cool team!”. If it is not the same display “Their rubbish team!”
Add an Elif statement to allow a third option.
For example use your second favourite team.
EXTENSION
Create the program in Python. Save it as elif2.
Start a new program using multiple ELIF statements. For example ask
someone what their favourite colour or animal is. Save the program as elif3.