当前位置:网站首页>3.2 multiple condition judgment
3.2 multiple condition judgment
2022-06-27 15:51:00 【leeshuqing】
Let's continue to introduce more complex conditional judgment statements .
We are moving forward again towards our predetermined goal . This time, we are ready to distinguish the excellent , So called excellence , Means greater than or equal to 90 branch . First try to write it in the most intuitive way of Chinese characters :

Corresponding Python The code is :
grade = int(input())
if grade >= 90:
print(' good ')
if grade >= 60 and grade < 90:
print(' pass ')
if grade < 60:
print(' fail, ')
This should not be difficult to understand . The most complicated one is the intermediate judgment condition , You should be able to guess , Yes ,and It means and , It is true only when both conditions are satisfied , That is, the conditions are met .
however , The code seems cumbersome , And the problem just now still exists , That is to say , If it has been judged as excellent , There is no need to judge the rest . First write a Chinese version :

Again using else It's written in Python Code :
grade = int(input())
if grade >= 90:
print(' good ')
else:
if grade >= 60 and grade < 90:
print(' pass ')
if grade < 60:
print(' fail, ')
Although nesting is a bit complicated , But we must ensure that the code is well indented , This code alignment can not only express the exact nested structure , It also helps us to see the structure of the program , Prevent potential errors .
Again, we will else The second condition inside also becomes else:
grade = int(input())
if grade >= 90:
print(' good ')
else:
if grade >= 60 and grade < 90:
print(' pass ')
else:
if grade < 60:
print(' fail, ')
Although the indentation is a little complicated , But the overall code runs very well , In any case, as long as there is one condition to pass the judgment , I will not make the following other judgments .
Can you simplify the code ? We boldly will else: And the following if In a row elif, Look at the effect :

The code is :
grade = int(input())
if grade >= 90:
print(' good ')
elif grade >= 60 and grade < 90:
print(' pass ')
elif grade < 60:
print(' fail, ')
Absolutely. , The effect is the same , Code guarantees high execution efficiency , At the same time, it is also very clear in typesetting . This actually constitutes multiple conditional judgments , in other words , Through multiple elif, You can continue to add more conditional judgments , Here is triple , Continue to add conditions , Four and five are OK , This is widely used in many code judgments .

So this is the way to think about it , We wrote the complete code :
grade = int(input())
if grade >= 90:
print(' good ')
elif grade >= 80 and grade < 90:
print(' good ')
elif grade >= 70 and grade < 80:
print(' in ')
elif grade >= 60 and grade < 70:
print(' pass ')
elif grade < 60:
print(' fail, ')
As mentioned above , Writing code is often not very difficult , But if you want to debug successfully, you can ensure that there are no errors , That requires experience and ability . Is there a problem with this code ?
If we type 101 perhaps -1 What will happen? ?

Although the program seems to output normally , But there is obviously something wrong with the results we input at this time , The correct program response should be prompted , Instead of still displaying the corresponding level .
Further modification , Join the necessary validation , This is the final code we have written so far :
grade = int(input())
if grade >= 90 and grade <= 100:
print(' good ')
elif grade >= 80 and grade < 90:
print(' good ')
elif grade >= 70 and grade < 80:
print(' in ')
elif grade >= 60 and grade < 70:
print(' pass ')
elif grade >= 0 and grade < 60:
print(' fail, ')
else:
print(' Wrong grade input ')
Last , Let's explain two common problems about the judgment conditions again :
First of all , Although it is generally used some such as greater than 、 Logical judgments such as equals and 、 Or such conditional operators to express these conditions , But as I said before ,Python The true and false boolean type of is actually an integer , So as long as the conditional result is not 0, It means that it is true , That is, the conditions are met .
Observe the following code output :
grade = int(input())
if grade:
print(grade)You will find that if you type 0, No output , But you enter any other value , The entered value will be displayed .


The reason is that ,grade by 0 Indicates that the condition is not met , Naturally, there is no output .
second , For floating-point numbers , When we compare , We cannot simply judge equality and inequality .
num = 1 / 3
if num == 0.33333333333333:
print('OK')According to more people's understanding , The two representations are a number . But you will find that the system may think that it is not equal , Without any output . The reason is easy to understand , because Python The default floating-point number has higher precision , There are indeed more decimal places than the decimal in the condition 3, Obviously bigger . in fact , In the vast majority 64 On a computer you enter 0.3333333333333333(16 individual 3) To replace the above decimal , You'll find that Python Consider that the conditions are met .

This obviously depends on the accuracy of the computer's representation of floating-point numbers .
therefore , A more reasonable floating-point number judgment should be carried out through a range of differences :
num = 1 / 3
if abs(num - 0.3333) <= 0.0001:
print('OK')Output is :OK. here abs Represents an absolute value function , It can ensure that the numerical change has an upper and lower error range .
Supporting learning resources 、 MOOC video :
Python Big data analysis - Shu Qing Li
https://www.njcie.com/python/
边栏推荐
- Centos8 PostgreSQL initialization error: initdb: error: invalid locale settings; check LANG and LC_* environment
- PSS:你距離NMS-free+提點只有兩個卷積層 | 2021論文
- sql注入原理
- Vscode uses yapf auto format to set the maximum number of characters per line
- About tensorflow using GPU acceleration
- Use of abortcontroller
- OpenSSF安全计划:SBOM将驱动软件供应链安全
- Openssf security plan: SBOM will drive software supply chain security
- AbortController的使用
- What are the characteristics of fixed income + products?
猜你喜欢

A distribution fission activity is more than just a circle of friends!

一场分销裂变活动,不止是发发朋友圈这么简单!
![Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration](/img/9f/64b0b83211bd1c615f2db9273bb905.png)
Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration

28 object method extension

域名绑定动态IP最佳实践

Vulnerability recurrence ----- 34. Yapi remote command execution vulnerability

What is the London Silver code

Centos8 PostgreSQL initialization error: initdb: error: invalid locale settings; check LANG and LC_* environment
![Beginner level Luogu 1 [sequence structure] problem list solution](/img/60/5e151ba31eb00374c73be52e3bfa7e.png)
Beginner level Luogu 1 [sequence structure] problem list solution
![洛谷_P1008 [NOIP1998 普及组] 三连击_枚举](/img/9f/64b0b83211bd1c615f2db9273bb905.png)
洛谷_P1008 [NOIP1998 普及组] 三连击_枚举
随机推荐
[digital signal processing] discrete time signal (analog signal, discrete time signal, digital signal | sampling leads to time discrete | quantization leads to amplitude discrete)
洛谷_P1007 独木桥_思维
2022-2-16 learning the imitated Niuke project - Section 6 adding comments
Teach you how to realize pynq-z2 bar code recognition
How is the London Silver point difference calculated
What is the London Silver unit
我想買固收+產品,但是不了解它主要投資哪些方面,有人知道嗎?
一场分销裂变活动,不止是发发朋友圈这么简单!
Indexeddb learning materials
守护雪山之王:这些AI研究者找到了技术的新「用武之地」
In the Alibaba cloud experiment, if the k8s forwards to port 3306 and the MySQL client is turned on, it will terminate abnormally. What is the reason?
E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)
About tensorflow using GPU acceleration
[digital signal processing] discrete time signal (discrete time signal knowledge points | signal definition | signal classification | classification according to certainty | classification according t
Can polardb-x be accessed through the client of related tools as long as the client supporting JDBC / ODBC protocol is afraid?
PolarDB-X开源版有没有支持 mysql5.7 的版本?
CNN convolutional neural network (the easiest to understand version in History)
SQL parsing practice of Pisa proxy
What are the characteristics of fixed income + products?
substrate 技术每周速览 20220411