Logical Operators
We are going to look at a few more operators, but these are not to do with mathematics, but logic.
Computers are only capable of comparing two items at one time and coming to a True or False conclusion. For example, look at this list and decide which is the largest number:
9, 22, 31, 4, 12, 20, 35, 50, 37
It probably didn't take you very long to notice that 50 is the largest number, and you likely did this by scanning across the whole list. The computer can not compare a whole list at one time like that. It has to look at two items at a time, remembering the largest one it has seen before coming to a conclusion.
Initially in the list above, it would compare the first two elements: 9 and 22. A condition to find the largest between the two would be written as follows:
if 9 > is greater than) 22 then remember 9, otherwise remember 22.
It would then compare the largest number out of the two with the next item in the list (so 22 and 31) and so on.
Here is a list of conditional operators a computer can use to compare two items:
Computers are only capable of comparing two items at one time and coming to a True or False conclusion. For example, look at this list and decide which is the largest number:
9, 22, 31, 4, 12, 20, 35, 50, 37
It probably didn't take you very long to notice that 50 is the largest number, and you likely did this by scanning across the whole list. The computer can not compare a whole list at one time like that. It has to look at two items at a time, remembering the largest one it has seen before coming to a conclusion.
Initially in the list above, it would compare the first two elements: 9 and 22. A condition to find the largest between the two would be written as follows:
if 9 > is greater than) 22 then remember 9, otherwise remember 22.
It would then compare the largest number out of the two with the next item in the list (so 22 and 31) and so on.
Here is a list of conditional operators a computer can use to compare two items:
Logical Operator |
Description |
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
>= |
Greater than or equal to |
< |
Less than |
<= |
Less than or equal to |
Conditional Statements
Using these conditional operators we can program computers to take actions based on a comparison. These are called if statements and allow us to write code that reacts to certain data rather than just a standard program that will always execute in the order of the instructions.
When a test condition is carried out, a Boolean is returned - either True or False.
When a test condition is carried out, a Boolean is returned - either True or False.
In this example we are comparing two variables; a and b. The test condition tests if a is less than b - if the test returns False, we know B is the larger number and we output a message. If this test fails, the code goes to the Else condition which then outputs that a is larger. In this example, a (5) is smaller than b (7) so it would output "B is the largest number".
Activities
Create a login program. Each one of these steps increase in difficulty. You may need to research some of these skills to find out how to complete tasks.
- Ask for a username and check that it is correct before outputting a welcome message. Output a failed login if the username is wrong.
- Add a password check too. Only if both the username AND password are correct should they be allowed to enter.
- The username check should not be case sensitive. It should be accepted if I enter "MyUsername" and it checks against "myusername".
Checking more than one condition
Sometimes we wish to check more than one condition. To do this we can use an ELSE IF condition, which in Python is represented as elif. This way we can check multiple conditions - really useful if we wish to write code for a menu for example. The conditions will be tested in the order they are written and if one returns true, the conditions after it will not be tested.
Activities
1. Create a results program for a teacher. It should allow the user to input their score:
2. Write a program to check if an input is negative, zero or positive
3. Write a small game that presents the user with a choice of two doors to go through. When they go through the door they should be presented with a further choice based on something in that room. For example:
Do you wish to enter door 1 or door 2?
>> 2
You have entered door 2. There is a dragon in this room.
Do you wish to:
1. Fight the dragon
2. Run away
>> 1
You are no match for the dragon, but do provide him with a good meal. Game over.
4. Check whether the number that has been input is divisible by 5 or 11 or not
- If they score 90 or over, output A*
- If they score 75 or over, output A
- If they score 65 or over, output B
- If they score 50 or over, output C
- If they score 40 or over, output D
- If they score 30 or over, output E
- If they score less than 30, output U
2. Write a program to check if an input is negative, zero or positive
3. Write a small game that presents the user with a choice of two doors to go through. When they go through the door they should be presented with a further choice based on something in that room. For example:
Do you wish to enter door 1 or door 2?
>> 2
You have entered door 2. There is a dragon in this room.
Do you wish to:
1. Fight the dragon
2. Run away
>> 1
You are no match for the dragon, but do provide him with a good meal. Game over.
4. Check whether the number that has been input is divisible by 5 or 11 or not
Assessment Task 1
1. Create a football score and points program.
- Input of two football team names
- Input the scores for each team
- Output the result (example: Man Utd 0-3 Crystal Palace)
- Output the points each team received:
- 3 points for a win
- 1 point for a draw
- 0 points for a loss
Going for Gold
|
|
Task A
- Download the two files and put them in the same folder
- Open studentfile.py
- Look at this line of code - what do you think the three numbers represent? Try and change them. Can you work out what each number controls in the code?
Task B
Task C
We have a problem. The rectangle now moves to the right of the screen, but does not stop. We want the rectangle to move to the right, bounce off the right wall and then move to the left, bounce off the left wall and move to the right... forever!
You have access to the current X position of the rectangle (posX), the current Y position (posY), the speed you set in setupRect() (speed) and a Boolean variable moveRight which you set to True in setupRect().
Can you change the code so the rectangle bounces off both walls? You will need to use another conditional...
- In setupRect() Try and change the rectangle variables and see what they all do
- Set moveRight = True and run the code... what happens?
Task C
We have a problem. The rectangle now moves to the right of the screen, but does not stop. We want the rectangle to move to the right, bounce off the right wall and then move to the left, bounce off the left wall and move to the right... forever!
You have access to the current X position of the rectangle (posX), the current Y position (posY), the speed you set in setupRect() (speed) and a Boolean variable moveRight which you set to True in setupRect().
Can you change the code so the rectangle bounces off both walls? You will need to use another conditional...