關于and
score = 85 if score>=90: ? ?print 'excellent' elif score>=80 and score<90: print="" elif="" scroe="">=60 and score<80: ? ?print 'passed' else: ? ?print 'failed' 這樣為什么不行呢
score = 85 if score>=90: ? ?print 'excellent' elif score>=80 and score<90: print="" elif="" scroe="">=60 and score<80: ? ?print 'passed' else: ? ?print 'failed' 這樣為什么不行呢
2017-12-18
舉報
2018-03-19
2017-12-18
score=85
if score>=90:
??? print 'excellent'
elif score>=80 and score<90:
??? print ""
elif score>=60 and score<80:
??? print 'passed'
else :
??? print 'failed'
這個程序可以運行,但是and用的沒必要,因為這是個嵌套的if-else,前面已經判斷了score>=90,后面elif的大條件已經是score<90了
2017-12-18
你這樣寫是沒錯的,但是if....elif....elif....else 是分層判斷的,當滿足一個條件時,就不會再去判讀下一個條件了,舉個例子,當score為85時,不滿足第一個條件,自然進入第二個條件判斷(也就是說,能夠進入第二判斷的先決條件是第一個條件不成立),所以只寫score>=80即可,如果再加上score<90則屬于重復判斷,本身無錯,但是程序執行效率降低。