流程控制之if判斷
1.什麽是if判斷
判斷一個條件如果成立則做...不成立則做...
2.為何要有if判斷
讓計算機能夠像人一樣具有判斷的能力
3.如何用if判斷
‘‘‘
#語法1:
‘‘‘
if 條件1:
code1
code2
code3
.......
‘‘‘
age=18
if age != 18:
print(‘你好啊小夥子‘)
print(‘加個微信吧...‘)
print(‘other code...‘)
# 語法2:
if 條件:
code1
code2
code3
......
else:
code1
code2
code3
......
#
age=22
sex = ‘male‘
x=‘dd‘
is cool = True
if age>16 and age<25 and\
x=‘dd‘ and is cool:
print(‘你好‘)
else:
print(‘再見‘)
#
# 語法3
if 條件1:
if 條件2:
code1
code2
code3
code2
code3
.......
age = 18
sex = ‘hemale‘
shencai = ‘good‘
is_beautiful = True
is_rich = True
if age >16 and age< 25 and sex = ‘hemale‘\
and shencai = ‘good‘ and is_beautiful:
print (‘表白‘)
if is_rich:
print(‘在一起‘)
else:
print(‘你是誰‘)
else:
print(‘8888888‘)
# 語法4
if 條件1:
子代碼塊1
elif 條件2:
子代碼塊2
elif 條件3:
子代碼塊3
elif 條件4:
子代碼塊4
.............
else:
子代碼塊5
#
score = input(‘your score:‘)
score = int(score)
if score >= 90:
print(‘A‘)
elif score >= 80:
print(‘B‘)
elif score >= 70:
print(‘C‘)
else:
print(‘laji‘)
流程控制之if判斷