elif是else if的簡寫。 else和elif語句也可以叫做子句,因為它們不能獨立使用,兩者都是出現在if、for、while語句內部的。 else子句可以增加一種選擇;而elif子句則是需要檢查更多條件時會被使用,與if和else一同使用。
實例:
people = 30 cars = 40 buses = 15 if cars > people: print( "We should take the cars.") elif cars < people: print ("We should not take the cars.") else: print ("We can't dicide.") if buses > cars: print ("That's too many buses.") elif buses < cars: print ("Maybe we could take the buses.") else: print ("We still can't decide.") if people > buses: print ("Alright, let's just take the buses.") else: print ("Fine, let's stay home then.")
運行結果:
#更多Python相關技術文章,請造訪Python教學欄位進行學習!
以上是python中的elif是什麼意思的詳細內容。更多資訊請關注PHP中文網其他相關文章!