官术网_书友最值得收藏!

if...elif...else

For multiple conditions you might be tempted to do something like this:

>>> if h > 50:
... print("Greater than 50")
... else:
... if h < 20:
... print("Less than 20")
... else:
... print("Between 20 and 50")
...
Between 20 and 50

Whenever you find yourself with an else-block containing a nested if  statement, like this, you should consider using Python's elif keyword which is a combined else-if.

As the Zen of Python reminds us, Flat is better than nested:

>>> if h > 50:
... print("Greater than 50")
... elif h < 20:
... print("Less than 20")
... else:
... print("Between 20 and 50")
...
Between 20 and 50

This version is altogether easier to read.

主站蜘蛛池模板: 靖远县| 阳谷县| 澄城县| 昌吉市| 临泽县| 万山特区| 马鞍山市| 万荣县| 麦盖提县| 庄河市| 广昌县| 获嘉县| 彭州市| 丹寨县| 长武县| 合江县| 丰城市| 新巴尔虎左旗| 永城市| 中宁县| 六枝特区| 九寨沟县| 当阳市| 澄江县| 兴仁县| 涡阳县| 焦作市| 广昌县| 营口市| 乌恰县| 祁东县| 舟曲县| 玛多县| 晋城| 洞头县| 阜南县| 页游| 托克逊县| 炉霍县| 襄垣县| 泗洪县|