Click on the link below for the complete T1 and T2 programme.
https://drive.google.com/file/d/0ByGWyKe-Ivm7bGl5NU5uMlU3S0E/view?usp=sharing
Thursday, 16 October 2014
10. Flow charts, AND, OR and NOT operators, truth tables: commandcolour codes in Python
Flowcharts
Flowcharts can be used to represent algorithms. Identify the flowchart symbols below.
Activity 9.2
Study this program.
# password checking
password=input("Please enter password: ")
newPassword=input("Please re-enter password: ")
if password == newPassword:
print("Access granted")
else:
print("Access denied")
Draw the flowchart for this program.
Activity 9.3
Study the flowchart.
Write a program to implement this flowchart.
Activity 9.4
Study the flowchart.
•
What does this flowchart do?
•
Write the program to implement this flowchart.
Extension: Can you extend the program to display whether they can watch 15 films or 18 films?
Activity 9.5
Boolean Operators
Complete the table using the logical (Boolean) operators AND, OR and NOT.
Symbol
|
Description
|
Returns true if both conditions are true.
| |
Returns true if any of the conditions are true.
| |
Reverses the outcome of the expression; true becomes false, false becomes true.
|
Activity 9.6
Predict the answer to the conditions and then use the interactive shell to test your answer.
Condition
|
Your prediction
(true or false?) |
Result
|
(78 == 10) or (6 == 7)
| ||
(78 == 10) or (6 == 6)
| ||
(78 == 10) and (6 == 6)
| ||
(1 < 10) and (2 < 10)
| ||
(1 < 10) or (2 < 10)
| ||
not ( 5 ==5)
| ||
not (6 < 4)
|
Hint: Try asking yourself the questions:
Is condition_1 true OR condition_2 true – if YES then the answer is true.
Is condition_1 true AND condition_2 true – if YES then the answer is true.
Activity 9.7
•
Make up some Boolean operator questions of your own and check your answers are correct by using the interactive Python shell.
Here is an example question:
>>>answer = 50
>>> (answer<40) or (answer >80)
True or False?
•
Try them out on other people. Make sure you can explain the right answer to them if they need you to.
Activity 9.8
A truth table lists all the possible combinations of true and false outcomes for each condition.
Complete the truth tables for AND and OR operators.
Truth table showing true and false AND conditions
Condition 1
|
Condition 2
|
Output
|
false
|
false
| |
true
|
false
| |
false
|
true
| |
true
|
true
|
Truth table showing true and false OR conditions
Condition 1
|
Condition 2
|
Output
|
false
|
false
| |
true
|
false
| |
false
|
true
| |
true
|
true
|
Activity 9.9
Write a program that asks the user to enter a year group and tells them which key stagethat year group is in.
Key stage
|
Year group
|
Key stage 1
|
Years 1 and 2
|
Key stage 2
|
Years 3, 4, 5 and 6
|
Key stage 3
|
Years 7, 8 and 9
|
Key stage 4
|
Years 10 and 11
|
Extension: What happens if the year group entered is greater than 11? Can you improve your program to cope with this?
Python commands colour coding
The Python language is colour coded.
Complete this table.
Colour coding
|
What does it show
|
Example
|
green
| ||
purple
| ||
black
| ||
orange
| ||
red
|
9. Selection constructs and writing readable code: elif
Leaning Outcomes
You will learn to:
Use ELIF
Write readable code
Activity 8.1
Using elif
This program simulates a fortune cookie. A random number is used to decide your ‘fortune’.
•
Copy and run this program.
# a random number is given by the randint() function
import random
answer= random.randint(1,6)
if answer == 1:
print("You will make a new friend this week")
elif answer == 2:
print("You will do well in your GCSEs")
elif answer == 3:
print("You will find something you thought you’d lost")
•
The program is not yet complete. Include your own ‘fortunes’ for the numbers 4, 5 and 6.
Note: random.randint(1,6) is a function that returns a random number between 1 and 6. The ‘import random’ command allows the program to access the random.randint() function.
Activity 8.2
Writing readable code: a style guide
Program code is read more often that it is written. Here are some guidelines on how to write Python code so that it is easy to read.
A style guide for Python code
•
Indent by four spaces for each indentation level.
•
Use blank lines to separate different parts of the program.
•
Use two blank lines to separate functions.
•
Choose meaningful names for variables, using CamelCase or with words separated by underscores.
•
Put imports at the top of the file.
•
Include one space around each side of an assignment and other operators.
•
Use complete sentences with the first word capitalised for comments.
•
Write comments that add clarity and explain what the program does. Do not simplyrepeat what the code already says.
•
Write function names in lowercase, with words separated by underscores.
•
Use meaningful function names which describe the purpose of the function.
•
Write constants in CAPITAL_LETTERS.
•
Use meaningful constant names, which describe the purpose of the constant.
Note: Functions are subprograms which start with the “deffunction_name():. Constants are variables whose value never changes. You will be covering functions in later lessons.
For more detail see http://www.python.org/dev/peps/pep-0008/
Apply the rules in the style guide above for this Python code.
def a(s):
if s<50:
print("You have lost")
else:
print("You have won")
Activity 8.3
Select some of your programs and make them more readable using the rules in the style guide.Lesson 9 activities
Thursday, 9 October 2014
8. Selection constructs and relational operators: if, then, else
Activity 7.1
Relational operators
Password checking is an example of a relational operator. If the password entered is the same as the password stored then the condition is true. The operator is ‘is equal to’.
Brainstorm other examples of condition statements and decide what the operator is.
Relation statement
|
Operator
|
Activity 7.2
•
Complete this table of the Python relational operators. Give an example of each and say whether it will evaluate to true or false.
Relational operator
|
Operator
|
Example
|
Evaluates to
|
Equal to
| |||
Not equal to
| |||
Greater than
| |||
Greater than or equal to
| |||
Less than
| |||
Less than or equal to
|
•
Try out your expressions by typing them into the Python interactive shell.
Activity 7.3
Greater than and less than
Find a way that works for you of remembering the difference between “less than” < and “greater than” >.
Hint: As there are only two options you only need to learn one!
Operator
|
>
|
<
|
Operator meaning
|
Greater than
|
Less than
|
How I remember this
|
Activity 7.4
This program asks if it is snowing and if so tells you to take a sledge otherwise to have a good day. The condition is missing.
Copy and complete the condition to make the program work correctly.
Activity 7.5
What condition is needed in this program to display ‘pass’ if the exam mark is 40 or more?
Copy and complete the condition to make the program work correctly.
Activity 7.6
Write a program that asks you to enter the colour of the light at a pedestrian crossing. If the light is green it tells you it is safe to cross, otherwise it tells you to stop.
Activity 7.7
Write a program that asks you for your password. It then asks you to re-enter your password. If they are the same the message “access granted” is displayed. If the passwords are not the same the message “access denied” is displayed.
Lesson 8 activities
Subscribe to:
Posts (Atom)