既然我們編程的目的是為了控制計算機能夠像人腦一樣工作,那么人腦能做什么,就需要程序中有相應的機制去模擬。人腦無非是數學運算和邏輯運算,對于邏輯運算,即人根據外部條件的變化而做出不同的反映,比如:
1 如果:女人的年齡>30歲,那么
age_of_girl=31 if age_of_girl > 30:print('阿姨好')
?2 如果:女人的年齡>30歲,那么:叫阿姨,否則:叫小姐
age_of_girl=18 if age_of_girl > 30:print('阿姨好') else:print('小姐好')
注意:
羅列以下幾種語法句型
語法1:
if 條件:
代碼1
代碼2
代碼3
...
age_of_bk=30print('start.....')inp_age=input('>>>: ') #inp_age='18'inp_age=int(inp_age)if inp_age == age_of_bk:print('猜對了')print('end.....')
語法2:
if 條件:
代碼1
代碼2
代碼3
...
else:
代碼1
代碼2
代碼3
...
age=38gender='male'is_beautiful=Trueif age >= 18 and age <= 25 and gender == 'female' and is_beautiful:print('開始表白。。。。')else:print('阿姨好')
語法3:
if 條件1:
代碼1
代碼2
代碼3
...
elif 條件2:
代碼1
代碼2
代碼3
...
elif 條件3:
代碼1
代碼2
代碼3
...
elif 條件4:
代碼1
代碼2
代碼3
...
else:
代碼1
代碼2
代碼3
...
如果:成績>=90,那么:優秀如果成績>=80且<90,那么:良好如果成績>=70且<80,那么:普通其他情況:很差
'''score=input('your score>>: ')score=int(score)if score >=90:print('優秀')elif score >=80:print('良好')elif score >=70:print('普通')
else:print('很差')
if 條件1:
if 條件2:
代碼1
代碼2
代碼3
...
代碼2
代碼3
age=18
gender='female'
is_beautiful=True
is_successful=Trueif age >= 18 and age <= 25 and gender == 'female' and is_beautiful:print('開始表白。。。。')if is_successful:print('在一起')else:print('我逗你玩呢。。。')
else:print('阿姨好')