3-1 python the if statement, if-else statement, if-elif-else statements
task
If you score 60 points or more is considered passed.
Bart assume scores of students is 75, please use the if statement to determine whether or not to print out passed:
1 #coding=utf-8 2 """ 3 python if语句使用 4 Author:liujiaqi 5 Date: 2019-09-18 6 """ 7 #Enter a codex 8 score = 75 9 if score >= 60: 10 print 'passed'
Both conditions judgment is "either-or", or meet the conditions 1, 2 or eligible, therefore, can use an if ... else ... statements unite them:
1 if age >= 18: 2 print 'adult' 3 else: 4 print 'teenager'
Use if ... else ... statements, we can True or False condition expression, if performed separately or else block code block.
Note: There is a back else ":."
task
If you score 60 points or more is considered passed, or as failed.
Bart assume scores of students is 55, please print out the if statement passed or failed:
1 #coding=utf-8 2 """ 3 python if ... else的使用 4 Author:liujiaqi 5 Date: 2019-09-18 6 """ 7 score = 55 8 if score >= 60: 9 print ('passed') 10 else: 11 print ('failed')
Sometimes an if ... else ... is not enough. For example, divided according to age:
1 Conditions 1:18 or older: Adult 2 Condition 2: 6 years or more: Teenager 3 Condition 3: 6 years: kid
To avoid nested structure if ... else ..., we can use if ... elif ... else ... more structure, once finished all the rules:
1 if age >= 18: 2 print 'adult' 3 elif age >= 6: 4 print 'teenager' 5 elif age >= 3: 6 print 'kid' 7 else: 8 print 'baby'
elif meaning is else if. As a result, we wrote structure is very clear set of conditions to determine.
Special Note: This series will determine the condition from top to bottom to determine if a judge is True, executing the corresponding code block, conditional on the back of directly ignored, no longer carried out.
task
If the score delineated in accordance with the results:
90 points or more: excellent
80 or more: good
60 minutes or more: passed
60 points or less: failed
Please write a program to print the results based on score.
1 #coding=utf-8 2 """ 3 python if-elif-else的用法 4 Author:liujiaqi 5 Date: 2019-09-18 6 """ 7 8 score = 85 9 10 if score >= 90: 11 print 'excellent' 12 elif score >= 80: 13 print 'good' 14 elif score >= 60: 15 print 'passed' 16 else: 17 print 'failed'