If…else statement
◤
How to use if … else statement
Start from if
Condition needs to be matched
Condition should end with :
If the condition is satisfied,
statement 1 will be executed.
Otherwise, statement will not be
performed
4 spaces
◤
If with operators
◤
If with operators
◤
If…else statement
Start from else
If the condition of if statement is not
satisfied, statement 2 will be
executed. Otherwise, statement 1
will be executed.
4 spaces
◤
If…else statement
◤
If … elif … else statement
Start from elif
If the condition of if statement Is not
satisfied, statement 2 will be
executed. Otherwise, statement 1
will be executed.
4 spaces
If the condition 1 and 2 are not satisfied,
statement 3 will be executed.
◤
If…elif … else statement
67
67
97
37
97
37
weight
weight
◤
Exercise
◤
Nested if … else statement
◤
Sometimes, if we can arrange nice order of statement, it may also work without nested one
◤
Try and except
No error occurs, it
will <do something>
If try will cause error, Exception error
will <handle the error>
◤
Try and except
We can also assign the type of error to except for control the output error message
◤
Try except finally
◤
Try except else finally
◤
Raise exception
◤
User defined exception
◤
Exercise
◤
Weather
sunny windy raining
happy sad
Blue
Black
Green
big
Red
small
White
◤
◤