python if語句里面怎么繼續寫if語句
f的基本語法格式看下面:第一行是條件語句,如果滿足條件就會執行第二行,沒有括號或者結束語句,比如endif,沒有。
假如第二行沒有縮進,就會產生錯誤。
新手容易犯一個錯誤就是條件語句后面不寫冒號,出現這樣的錯誤:
我們假如有多個條件,我們可以使用else,當條件不滿足的時候執行它下面的語句塊。當然else是頂個寫,并且后面記得寫冒號。
如果還有更多的條件,我們可以使用elif,同樣不要忘記冒號和縮進
Python語言中if語句的問題寫一個程序
def isLeapYear(year): if year % 400: if year % 4: # 不能被4整除 return False elif year % 100: # 能被4整除但不能被100整除 return True else: return False else: # 能被400整除則為閏年 return True# ==> 簡化邏輯 ==>def isLeapYear(year): if year % 400: return (year % 4 == 0) and (year % 100) else: # 能被400整除則為閏年 return True。
python else if 怎么表示
s = ['a', 'b', 'c', 'd']
s[*('c')] = 'chinese'
print s
print 'd' * 80
for index, value in enumerate(s):
if 'd' in value:
s[index] = 'Japan'
elif 'b' in value:
s[index] = 'China'
else:
pass
print s
轉載請注明出處華閱文章網 » pythonif語句怎么寫