Total Pageviews

2016/03/08

[Python] IF...ELIF...ELSE Statements

Example
Assume I have four exam type, each type has its passing scores. 
I will provide a method, as people fill in exam type and scores, it will you tell you pass the exam or not. 
This example will demonstrate how to use if / elif / else in Python program:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
""" 判斷分數是否通過考試 """
def passExan(examType, scores):
 if examType == '1':
  if scores >= 252.79:
   print("恭喜您通過 專業1→營運職2 升等考試")
  else:
   print("很抱歉,您未通過 專業1→營運職2 升等考試")
 elif examType == '2':
  if scores >= 248.65:
   print("恭喜您通過 專業2→專業1 升等考試")
  else:
   print("很抱歉,您未通過 專業2→專業1 升等考試")
 elif examType == '3':
  if scores >= 260.79:
   print("恭喜您通過 專業3→專業2 升等考試")
  else:
   print("很抱歉,您未通過 專業3→專業2 升等考試")
 elif examType == '4':
  if scores >= 254.88:
   print("恭喜您通過 專業4→專業3 升等考試")
  else:
   print("很抱歉,您未通過 專業4→專業3 升等考試")
 else:
  print("查無此考試項目")

Test:
1
2
3
4
5
6
7
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import myUtil
>>> myUtil.passExam("1", 270)
恭喜您通過 專業1→營運職2 升等考試
>>> myUtil.passExam("3", 180)
很抱歉,您未通過 專業3→專業2 升等考試


Reference
[1] http://www.tutorialspoint.com/python/python_if_else.htm

No comments: