Condition Selection,
IfStatement Structure
IfJudgment condition:
Code to be executed
Condition: It is generally a relational expression or a bool value.
Execution Process: when the program runs at the if clause, it first determines the conditions contained in the if clause. if the condition is True, True is returned, and the code contained in the if clause is executed. if the condition is not True, if the returned value is False, skip the if statement and continue the execution.
Example1:
Enter Mr. Wang (Chinese, English, and mathematics) score in the console application (out of 100 for a single subject)
Judgment:
1) if the average score is greater than or equal to 90, you are a smart child.
2) If the average score is less than 60, you will be reminded that your score is not satisfactory and you should work hard in the future!
Chinese = int (input ("Enter the Chinese score:"))
Maths = int (input ("Enter the math score:"))
Englist = int (input ("Enter the English score:"))
Avg_result = (chinese + maths + englist)/3
#Select conditions for use--Greater than or equal90Minute
IfAvg_result> = 90:
Print ("Your average score:%. 2f,Really a smart child!"% Avg_result)
#Select conditions for use--Less60Minute
IfAvg_result <60:
Print ("Your average score:%. 2f,You have to work hard!"% Avg_result)
#Note:IfNote the indentation of statements executed by traditional Chinese medicine!
Result:
C: \ python \ python.exe C:/python/demo/file2.py
Enter the Chinese score: 57
Enter the math score: 57
Enter the English score: 59
Your average score is 57.67. You have to work hard!
Process finished with exit code 0
Example2(Small safflower case)
Enter Mr. Wang (Chinese, English, and mathematics) score in the console application (out of 100 for a single subject)
Judgment:
1) if one course is 100 points
2) if there are two doors greater than 90 points
3) if the number of the three doors is greater than 80
To meet the above conditions, reward a small red flower
Chinese = int (input ("Enter the Chinese score:"))
Maths = int (input ("Enter the math score:"))
Englist = int (input ("Enter the English score:"))
Get_course =""
If(Chinese = 100OrMaths = 100OrEnglist = 100 ):
If(Chinese = 100): get_course + ="Chinese,"
If(Maths = 100): get_course + ="Mathematics,"
If(Englist = 100): get_course + ="English,"
Print ("Your% SCome on100Reward a small red flowerBytes!"% Get_course)
If(Chinese> = 90AndMaths> = 90)Or(Chinese> = 90AndEnglist> = 90)Or(Maths> = 90AndEnglist> = 90 ):
If(Chinese> = 100): get_course + ="Chinese,"
If(Maths> = 90): get_course + ="Mathematics,"
If(Englist> = 90): get_course + ="English,"
Print ("Your% SGreater90Reward a small red flowerBytes!"% Get_course)
If(Chinese> = 80AndMaths> = 80AndEnglist> = 80 ):
Print ("Your three subjects include Chinese, mathematics, and English80Reward a small red flowerBytes")
Result:
C: \ python \ python.exe C:/python/demo/file2.py
Enter the Chinese score: 100
Enter the math score: 98
Enter English score: 80
Your Chinese Language scored 100 points and rewarded a small red flower!
Your Chinese, Chinese, mathematics, and more than 90 points will reward a small red flower!
Your three subjects, including Chinese, mathematics, and English, all scored more than 80 points.
Process finished with exit code 0