# Python IF，ELIF和ELSE语句

## 简单的if语句

``````score_theory = 40
score_practical = 45
if(score_theory + score_practical > 100):
print("Invalid score. Please check the input.")
``````

``````score_theory = 40
score_practical = 45
if score_theory + score_practical > 100: # End of condition
print("Invalid score. Please check the input.")
``````

``````score_theory = 50
score_practical = 55
if(score_theory + score_practical >= 100):
print("Invalid score. Please check the input.")
``````
``````Invalid score. Please check the input.
``````

## 一次测试：if-else语句

if-else语句用于编码的方式与使用英语的方式相同。 if-else语句的语法为：

``````score_theory = 40
score_practical = 45
if(score_theory + score_practical > 100):
print("Please check the input. Score exceeds total possible score.")
else:
print("Score validated. Your total is: ", score_theory + score_practical)
``````
``````Score validated. Your total is:  85
``````

``````score_theory = 40
score_practical = 45
# Single statement: if(condition): Statement for when condition is true
if (score_theory + score_practical > 100): print("Please check the input. Score exceeds total possible score.")
# Single statement: if(condition): Statement for when condition is false
else: print("Score validated. Your total is: ", score_theory + score_practical)
``````
``````Score validated. Your total is:  85
``````

``````score_theory = 60
score_practical = 40
if(score_theory + score_practical > 100):
print("Please check the input. Score exceeds total possible score.")
else:
print("Score validated. Your total is: ", score_theory + score_practical)
``````
``````Score validated. Your total is:  100
``````

## 多种测试：if-elif-else语句

• 2.1检查score_theory
• 2.2检查score_practical

``````# Step 1
score_theory = 60
score_practical = 20

if(score_theory > 50):
print("Please check the input score for 'Theory'.") # Step 2.1
elif(score_practical > 50):
print("Please check the input score for 'Practical'.")  # Step 2.2
else:
print("Score validated. Your total is: ", score_theory + score_practical) # Step 3
``````
``````Please check the input score for 'Theory'.
``````

if-else-if语句后面的语法是：

if(Condition1)：

Condition1的缩进语句块

ELIF(条件2)：

Condition2的缩进语句块

``````score_theory = 60
score_practical = 20

if(score_theory > 50):
print("Please check the input score for 'Theory'.")
else:
if(score_practical > 50):
print("Please check the input score for 'Practical'.")
else:
print("Score validated. Your total is: ", score_theory + score_practical)
``````
``````Please check the input score for 'Theory'.
``````

elif语句实际上使编写代码更容易。想象一下, 如果问题语句变得更加复杂, 那么跟踪另一个if语句中的每个if-else语句就很容易成为噩梦！

## 嵌套if语句

``````coursework = "English"
score_theory = 53
score_practical = 35

if(coursework == "Science" or coursework == "science"):
if(score_theory > 50):
print("Please check the input score for 'Science: Theory'.")
elif(score_practical > 50):
print("Please check the input score for 'Science: Practical'.")
else:
print("Score validated for Science. Your total is: ", score_theory + score_practical)
elif(coursework == "English" or coursework == "english"):
if(score_theory > 60):
print("Please check the input score for 'English: Theory'.")
elif(score_practical > 40):
print("Please check the input score for 'English: Practical'.")
else:
print("Score validated for English. Your total is: ", score_theory + score_practical)
else: print("Coursework not recognised. Please enter score for either Science or English.")
``````
``````Score validated for English. Your total is:  88
``````

``````coursework = "English"
score_theory = 53
score_practical = 78

if(coursework == "Science" or coursework == "science"):
if(score_theory > 50 or score_practical > 50):
# Can't distinguish the error in Science: theory or Science: Practical
print("Please check the input score for Science.")
else: print("Score validated for Science. Your total is: ", score_theory + score_practical)
elif(coursework == "English" or coursework == "english"):
if(score_theory > 60 or score_practical > 40):
# Can't distinguish the error in English: theory or English: Practical
print("Please check the input score for English.")
else: print("Score validated for English. Your total is: ", score_theory + score_practical)
else: print("Coursework not recognised. Please enter score for either Science or English.")
``````
``````Please check the input score for English.
``````

• 回顶