Python learning basics (learning Python) -- 3.1Python if branch statement

Source: Internet
Author: User

This section describes the if branch statements.

The if branch statement is a logical condition control statement in Python that is used to control certain statements in conditional execution. When the condition conditon after the if condition is met, the statement block under the if condition is executed, however, when the if control condition conditon is not met, the statement block under the if statement is not executed. The syntax structure of if is as follows.

[Python]
If conditon:
(Tab) statement1
(Tab) statement2
(Tab) statement3
Etc.

If conditon:
(Tab) statement1
(Tab) statement2
(Tab) statement3
The logical flowchart of the etc. if condition Control Branch statement, as shown in.

 
 

 

The program below is to calculate the average value of three courses. If the average score is greater than 90, print the word congratulations. The input_info function implements the input of three course scores and calls process_info to calculate the average value. After calculating the average value (8th rows) in process_info, it determines whether the average value is greater than 90 (9th rows ).

 

[Python]
Def input_info ():
Mat = int (raw_input ("plz input your mathtic record :"))
Eng = int (raw_input ("plz input your english record :"))
His = int (raw_input ("plz input your history record :"))
Process_info (mat, eng, his)
 
Def process_info (m, e, h ):
Ave = (m + e + h)/3
If ave> 90:
Print ("Congratulation! Ave = ", ave)
 
Def main ():
Input_info ()
 
Main ()

Def input_info ():
Mat = int (raw_input ("plz input your mathtic record :"))
Eng = int (raw_input ("plz input your english record :"))
His = int (raw_input ("plz input your history record :"))
Process_info (mat, eng, his)

Def process_info (m, e, h ):
Ave = (m + e + h)/3
If ave> 90:
Print ("Congratulation! Ave = ", ave)

Def main ():
Input_info ()

Main ()

Shows the running result when the average value of 3 numbers is greater than 90.
 

Shows how to run an average value of 3 to 90.

 

 
 

From the two running results, if the calculated average ave (8th rows) is greater than 90 (9th rows), then the print statement under the if (9th rows) Statement is 10th rows) run. if the calculated average ave value is less than 90, the print Statement (9th rows) below if (10th rows) is not executed ).
In this example, we can see that if can control the execution of certain statement blocks by conditions.


 

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.