if else 和if elif 的区别

所有的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日

出处:https://www.cnblogs.com/zdwu/p/8072438.html

你可能感兴趣的:(if else 和if elif 的区别)