September 2013
Intermediate to advanced
350 pages
9h 38m
English
An if statement’s block can contain any type of Python statement, which implies that it can include other if statements. An if statement inside another is called a nested if statement.
| | value = input('Enter the pH level: ') |
| | if len(value) > 0: |
| | ph = float(value) |
| | if ph < 7.0: |
| | print(ph, "is acidic.") |
| | elif ph > 7.0: |
| | print(ph, "is basic.") |
| | else: |
| | print(ph, "is neutral.") |
| | else: |
| | print("No pH value was given!") |
In this case, we ask the user to provide a pH value, which we’ll initially receive as a string. The first, or outer, if statement checks whether the user typed something, which determines whether we examine the value of pH with the inner if statement. (If the user didn’t enter ...
Read now
Unlock full access