Python Chapter 4.1
Python Chapter 4.1
▪ Selection/Decision
▪ Statements are executed conditionally. Executed when the condition is true and skipped
when the condition is false
▪ Looping/Iteration/Repetition
▪ Statements are repeated conditionally
S
E
…consecutive steps (or
Q
groups of steps) processed
U one after another in the order
that they arise.
A bit like reading through a cooking
E recipe, one line at a time.
N
C E
Tru
e
False
Tru
e
False
Tru
e
False
▪ Explain simple if structure and how it works
▪ Write simple if statement
▪ Evaluate simple if statement
False
if <condition>: <condition
>
Statement1
Statement2
:
Execute if the condition is
true True
StatementN
Uncondition Statement 1
:
Either <condition> True of
Statement False, execute this statement StatementN
Uncondition Statement
▪ Python codes
<condition>
>>>if x+y==z:
print(“{}+{}={}”.format(x,y,
z))
>>>if not(x%2==0):
print(x,” is even”)
>>>if y%2==0:
print(y,” is even”)
Task 1
1) Create a program that prompts the user for their age
• If their age is less than 14 it should display message
“Too Young”
• If their age is greater than or equal to 14, it should produce a
A message “Eligible”
Create your program in SCRIPT mode and save it as IF1
Task 2
1) Create a program that prompts the user for their NAME and the YEAR
they were born.
• If their year is after 1999, the program should output
‘Welcome NAME, you are a 21st Century Child!’
• If their year is not after 1999, the program should output
‘Welcome NAME, you are a 20th Century child!’
Create your program in SCRIPT mode and save it as IF2
Task 3
1) Create a program that prompts the user for a PASSWORD
• If the entered password is less than 8 characters long, it should output
‘Password Rejected.’
• If the entered password is at least 8 characters long, it should output
‘Password Accepted.’
Create your program in SCRIPT mode and save it as IF3