對(duì)比以下兩種寫(xiě)法,思考一下為何可以這樣寫(xiě)。
成績(jī)?cè)?[0,50)、[50,60)、[60,80)、[80,100)、100、其它
1
2
3
4
5
6
7
8
9
10
11
12
13
|
score = float ( input ( "請(qǐng)輸入你的成績(jī):" )) if score = = 100 : print ( '666呀,走吃大餐去' ) elif 80 < = score < 100 : print ( '還行,優(yōu)秀,走,喝飲料去' ) elif 60 < = score < 80 : print ( '加油呀,弄明白點(diǎn)' ) elif 50 < = score < 60 : print ( '這可有點(diǎn)浪喲' ) elif 0 < = score < 50 : print ( '學(xué)不懂嗎?' ) else : print ( '你輸入的是啥?' ) |
80 <= score < 100 為何可以寫(xiě)成 score >= 80 ? 上一條語(yǔ)句不滿(mǎn)足時(shí)往下執(zhí)行,這時(shí) score <100 就不需要了;
注意: 如果把這些條件表達(dá)式的順序換下,那么這種寫(xiě)法是錯(cuò)誤的。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
score = float ( input ( "請(qǐng)輸入你的成績(jī):" )) if score = = 100 : print ( '666呀,走吃大餐去' ) elif score > = 80 : print ( '還行,優(yōu)秀,走,喝飲料去' ) elif score > = 60 : print ( '加油呀,弄明白點(diǎn)' ) elif score > = 50 : print ( '這可有點(diǎn)浪喲' ) elif score > = 0 : print ( '學(xué)不懂嗎?' ) else : print ( '你輸入的是啥?' ) |
利用break進(jìn)行程序運(yùn)行時(shí)間的優(yōu)化
在循環(huán)語(yǔ)句中,可以用break來(lái)退出不必要繼續(xù)執(zhí)行的循環(huán)
有break后
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/FlyingLiao/p/11147220.html