所有的if 后的命令都会运行,而elif后面的命令是根据上一个if是否运行,如果运行了,elif则略过,否则才运行。
def analyzeAge( age ):
if age < 21:
print "You are a child"
if age > 21:
print "You are an adult"
else: #Handle all cases were 'age' is negative
print "The age must be a positive integer!"
analyzeAge( 18 ) #Calling the function
>You are a child
>The age must be a positive integer!
def analyzeAge( age ):
if age < 21:
print "You are a child"
elif age > 21:
print "You are an adult"
else: #Handle all cases were 'age' is negative
print "The age must be a positive integer!"
analyzeAge( 18 ) #Calling the function
>You are a child
2019年6月8日