You can also learn Python ---- if and python ---- if

Source: Internet
Author: User

You can also learn Python ---- if and python ---- if

Introduction: if the execution process exceeds two branches, the if-elif statement is used.

If-elifStatement Structure

If condition:

Code to be executed

Elif judgment conditions:

Code to be executed

......

Else:

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 to be executed is executed;

If the conditions are not true, judge the elif conditions sequentially. If the conditions are met, run the corresponding code. If none of the conditions are true, run the code to be executed under else.

 

Example1: Enter Mr. Wang's (Chinese, English, and mathematics) score (out of 100 for a single subject) to judge the score rating

The student evaluation criteria are as follows:

  • Score> = 90:
  • 90 points> score> = 80 points: B
  • 80 points> score> = 70 points: C
  • 70 points> score> = 60 points: D
  • Score <60: E

 

Chinese_result = int (input ("Enter the Chinese score:"))

Maths_result = int (input ("Enter the math score:"))

Englist_result = int (input ("Enter the English score:"))

Avg_result = (chinese_result + maths_result + englist_result)/3

IfAvg_result> = 90:

Print ("Your average score is:%. 2f,The sum of scores is:A"% Avg_result)

ElifAvg_result> = 80AndAvg_result <90:

Print ("Your average score is:%. 2f,The sum of scores is:B"% Avg_result)

ElifAvg_result> = 70AndAvg_result <80:

Print ("Your average score is:%. 2f,The sum of scores is:C"% Avg_result)

ElifAvg_result> = 60AndAvg_result <70:

Print ("Your average score is:%. 2f,The sum of scores is:D"% Avg_result)

Else:

Print ("Your average score is:%. 2f,The sum of scores is:E"% Avg_result)

 

Result:

C: \ python \ python.exe C:/python/demo/file2.py

Enter the Chinese score: 45

Enter the math score: 34

Enter English score: 56

Your average score is 45.00, and the total score is: E

 

Process finished with exit code 0

 

Example2(The second optimization in the previous 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 =""

IfChinese = 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)

Elif(Chinese> = 90AndMaths> = 90)Or(Chinese> = 90AndEnglist> = 90)Or(Maths> = 90AndEnglist> = 90 ):

If(Chinese> = 90): get_course + ="Chinese,"

If(Maths> = 90): get_course + ="Mathematics,"

If(Englist> = 90): get_course + ="English,"

Print ("Your% SGreater90Reward a small red flowerBytes!"% Get_course)

ElifChinese> = 80AndMaths> = 80AndEnglist> = 80:

Print ("Your three subjects include Chinese, mathematics, and English80Reward a small red flowerBytes")

Else:

Print ("No small red flowersBytesNext time, try again!")

 

Result:

C: \ python \ python.exe C:/python/demo/file2.py

Enter Chinese score: 87

Enter the math score: 86

Enter English score: 91

Your three subjects, including Chinese, mathematics, and English, all scored more than 80 points.

 

Process finished with exit code 0

 

Question: if-and if-else are available, why do we still need if-elif-else??

 

The selection of conditions on the left is four parts, and the next two statements are a whole. The three if statements add an if-else, And the else matches the nearest if statement.

The right side is a whole, and only one statement can be used during execution.

 

Example3:Enter a month to determine which season the month belongs:

Winter (12-2 months) Spring (3-5 months) Summer (6-8 months) Autumn (9-11 months)

Month = int (input ("Enter a month:"))

If(Month = 12OrMonth = 1OrMonth = 2 ):

Print ("% DThe month is winter"% Month)

Elif(Month = 3OrMonth = 4OrMonth = 5 ):

Print ("% DThe month is spring"% Month)

Elif(Month = 6OrMonth = 7OrMonth = 8 ):

Print ("% DMonth is summer"% Month)

Elif(Month = 9OrMonth = 10OrMonth = 11 ):

Print ("% DThe month is autumn."% Month)

 

Result:

C: \ python \ python.exe C:/python/demo/file2.py

Enter a month: 8

August is summer

 

Process finished with exit code 0

 

I have original articles, which can be reproduced at will for non-commercial purposes. Please retain the source of the original article for reprinting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.