可以使用在if语句后直接添加else的语法格式,或者使用嵌套if语句。例如:
if condition:
# do something
else:
# do something else
对于已经存在的if语句:
if x > 0:
print("x is positive")
可以添加else语句:
if x > 0:
print("x is positive")
else:
print("x is not positive")
if condition1:
# do something
else:
if condition2:
# do something else
对于已经存在的if语句:
if x > 0:
print("x is positive")
可以嵌套if语句:
if x > 0:
print("x is positive")
else:
if x == 0:
print("x is zero")
else:
print("x is negative")