在编程中,当条件语句在另一个语句中出现时,使用嵌套一词。在Python中,新的if语句可以嵌套在if以及else块中。您必须知道,每个if块的缩进级别都在增加。因此,在嵌套if条件的情况下,内部块的缩进级别将从外部块增加。嵌套if的通用语法如下:
if expr1==True: if expr2==True: #this block will be executed when expr1 and expr2 are true else: #this block if expr is true but expr2 is false else: if expr3==True: #if expr1 is false and expr3 is true else: #when expr1 and expr3 are false