Python basics of developing a simple if statement and examples of usage

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45523154/article/details/102730329

Select the condition is the most basic and most important structure in a statement Python syntax.

Not merely procedural, normal life, we will face a lot of choices, such as rain today, I was driving to work? Or do the bus? Today at noon to eat noodles or eat salted fish? So the program also faced this problem, this value should be greater than 100 what to do, what action is less than 100? Python conditional expressions introduced in the previous article, also wrote the standard conditional statements. So in the end what conditions are selected branches to choose from, the face of a branch, two branches, or more branches when selecting what kind of statement to achieve.

** Examples 01: ** student achievement programming input language, mathematics, English, Billing Division out of 100

1 judge, if the average is greater than or equal to 90 points 90 points, just Hint: You're so smart!

2 judgment, if the average is less than 60 minutes, it prompts: You have to work hard!

Operation Code:

Int = Chinese (the INPUT ( "Enter the student's language performance:" ))

Int = Maths (the INPUT ( "Please enter the students' mathematics achievement:" ))

= Int English (the INPUT ( "Please enter student achievement in English:" ))

average = (Chinese + Maths + English) / 3

**if **average >= 90:

print (** "The average student is:.!% 2f You're smart" **% average)

**if **average < 60:

print (** "The average student is:.!% 2f you have to work hard" **% average)

Code demonstrates:

Note: if block of code to be executed must be indented

Code analysis:

First, there needs to input three subjects results, the use of input and student performance input directly to an integer int; secondly define a variable to calculate the average of the three subjects, if judged by whether or not the final average student is 90 points, or less than 60 minutes, and displays the result of the judgment.

** Examples 02: ** achieve student achievement input language, mathematics, English in the console application with the program, out of 100 single subject

Analyzing conditions:

1) If there is a test score is 100 points

2) If there are two test scores more than 90 points

3) If the three test scores greater than 80 points

Meet any of the three conditions is, on the award of a small red flower

Operation Code:

Int = Chinese (the INPUT ( "Enter the student's language performance:" ))

Int = Maths (the INPUT ( "Please enter the students' mathematics achievement:" ))

= Int English (the INPUT ( "Please enter student achievement in English:" ))

course = ""

**if **(Chinese == 100 **or **Maths == 100 **or **English == 100):

if(Chinese == 100): course += "语文、"

if(Maths == 100): course += "数学、"

if(English == 100): course += "英语、"

print(**“你的%s考了100分,奖励一朵小红花!” **% course)

**if **(Chinese >= 90 **and **Maths >= 90) **or **(Chinese >= 90 **and **English>= 90) **or **(Maths >= 90 **and **English >= 90):

if(Chinese >= 100): course += "语文、"

if(Maths >= 90): course += "数学、"

if(English >= 90): course += "英语、"

print(**“你的%s成绩大于90分,奖励一朵小红花!” **% course)

**if **(Chinese >= 80 **and **Maths >= 80 **and **English >= 80):

print(“你的语文、数学、英语三门成绩都大于80分,奖励一朵小红花”)

对Python感兴趣或者是正在学习的小伙伴,可以加入我们的Python学习扣qun:784758214,看看前辈们是如何学习的!从基础的python脚本到web开发、爬虫、django、数据挖掘等,零基础到项目实战的资料都有整理。送给每一位python的小伙伴!每天都有大牛定时讲解Python技术,分享一些学习的方法和需要注意的小细节,点击加入我们的 python学习者聚集地

代码演示:

代码分析:

首先定义三个考试科目的变量,并把输入的学生成绩通过int转换为整型;

第一个if语句判断三门成绩是否有考100分的,如果有就添加到变量course里面,并打印出来;

第二个if语句判断三门成绩是否有两门成绩>=90分的,如果有就添加到变量course里面,并打印出来;

第三if语句判断三门科目的考试成绩是否都>=80分,如果有就添加到变量course里面,并打印出来;

所以会发现如果单纯的通过if语句来实现需求就会出现如图的情况,每个条件都满足,都会分别打印并显示出来。如何解决?后面会继续给大家分享Python的条件选择更多的应用方法,请继续关注。

总结:if 语句结构

if 判断条件:

要执行的代码

1)if判断条件后面一般是比较运算符链接的表达式,中间可能还有逻辑运算等,判断语句后面冒号不要忘记;

2)如果满足某个条件就执行下面的代码块,如果不满足就直接忽略。所以在执行过程中程序运行到if处,首先判断if所带的条件,如果条件成立,就返回True,则执行if所带的代码;如果条件不成立,就返回值是False, 跳过if语句继续向下执行。

Guess you like

Origin blog.csdn.net/weixin_45523154/article/details/102730329