Python by a conditional statement or statements of the execution result (True or False) to determine the execution code block.
Keyword: if ... elif ... else ...
if can stand alone, elif and else an error will not be alone:
- if...
- if...elif...
- if...else...
- if ... elif ... elif ... else
note:
1, each of the conditions to be used later in the colon: indicates the following condition is satisfied after the statement block to be executed.
2, using indentation divided statement blocks, the number of statements in the same indentation together to form a block.
3, there is no switch in Python - case statement.
# Exercise: Write a program to determine the type of result
Score = int >>> (the INPUT ( " Please enter your score: " )) Please enter your score: 62 >>> IF Score> 90 : ... Print ( " Excellent " ) ... elif 60 <= Score <= 90 : ... Print ( " good " ) ... the else : ... Print ( " poor " ) ... good
if nested
In the nested if statements, can if ... elif ... else in another configuration if ... elif ... else structure
Example:
int = NUM (INPUT ( " Enter a number: " )) IF NUM% 2 == 0: IF NUM%. 3 == 0: Print ( " the number you entered is divisible by 2 and. 3 " ) the else : Print ( " you the number can be divisible by 2, but not divisible by 3 " ) the else : IF NUM% 3 == 0: Print ( " the number you enter is divisible by 3, but not divisible by 2 " ) the else : Print ( " the number you entered can not be divisible by 2 and 3 " )