Monday, 15 October 2012

If, For, While, Random, Break, Boolean Expressions

If Statements
An if statement will run the code if a criteria is met.
See here for an example.

When you are checking the values, you can use the following operators:
== - equals to
!= - not equals to
> - greater than
< - less than
>= - greater than or equal to
<= - less than or equal to


For Loops
A for loop will repeat the code inside of it a certain amount of times.
You can define how often the code is run by changing the numbers in the brackets.
for x in range (0, 10):
  print("Hello world")
In the example above x goes up by 1 each time until the whole code has been repeated 9 times.


While...
A while statement is used because you want something to run if a condition is met, or until a condition is no longer met.
num = int(input("Please enter a number: "))
while num != 5:
  print("The number is not 5")
  num = int(input("Please try another number :))
In the example above the user inputs a number. While the number they enter is not 5, the program outputs a message and then asks them to try again. The value in num is then changed to what the user types in the second time and the while statement is run again.
If the program did not ask for the user to input the number again after the output message, the while statement would just output the message "The number is not 5" forever....

You can use the same operators as you can for if in a while statement.

Import Random
Sometimes you want the computer to choose a number at random for you.
You do this by importing the random number generator (a lot like when we imported the turtle this needs to go right at the top).
import random
myNum = random.randint(0, 5)
The code above will randomly pick a number between the values in the brackets, and save whatever number it picks in the variable myNum.


Break
This is how you get a program to stop running.
break
Any code after this will not be run in your program - so be careful where you use it!

Boolean Expressions
Boolean expressions are how the computer checks if a value is true or false. You can also use boolean expressions to check if two or more values are the same or not.
With an if statement we checked one value with symbols, with boolean expressions we check two or more values with 'and' and 'or'.
The example below uses logging onto the school network as an example, although the true and false options stop the same, regardless of the circumstances.

EXAMPLE: Username and password - in order to log onto the school network both your user name and password need to be correct.
Username AND password.
If you type both of them in correctly then the computer sees the answer as being True and so it lets you log on.
If username is correct but password is incorrect, it sees the answer as being False and wont let you log on.
If username is incorrect but password is correct, it sees the answer as being False and wont let you log on.
If username is incorrect and password is incorrect, it sees the answer as being False and wont let you log on.

Username and password - lets pretend that you only need either your username or your password to be correct in order to log on. It doesn't matter if one is wrong so long as one is right.
Username OR password.
If you type both of them in correctly then the computer sees the answer as being True and so it lets you log on.
If username is correct but password is incorrect, it sees the answer as being True and so it lets you log on.
If username is incorrect but password is correct, it sees the answer as beingTrue and so it lets you log on.
If username is incorrect and password is incorrect, it sees the answer as being False and wont let you log on.


How to use boolean expressions in your code...
num1 = int(input("Please input a number: "))
num2 = int(input("Please input a second number: "))
if num1 == 4 and num2 == 6:
  print("Well done you must be psychic!")
if num1 == 4 or num2 == 6:
  print("Half way there!")
The code above asks the user to input two numbers. These are then stored in the variables num1 and num2.
An if statement then checks what they have typed in against some criteria.
On like 3, the if statement uses the "and" boolean operator to check if both numbers are the same as the ones in the program. In order for the program to consider this statement 'True' (and therefore run the code underneath it) both of the number have got to be the same as the ones saved in the program - in that order.
On link 5, the if statement uses the "or" boolean operator to check if either of the numbers are the same as the ones stored in the program. If they are correct with either of them the program will print out the "Half way there" message.

No comments:

Post a Comment