当前位置:网站首页>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/
边栏推荐
- OpenSSF安全计划:SBOM将驱动软件供应链安全
- 2022年最新《谷粒学院开发教程》:8 - 前台登录功能
- I want to buy fixed income + products, but I don't know what its main investment is. Does anyone know?
- E modulenotfounderror: no module named 'psychopg2' (resolved)
- On traversal of tree nodes
- Let's talk about the process of ES Indexing Documents
- 如果想用dms来处理数据库权限问题,想问下账号只能用阿里云的ram账号吗(阿里云的rds)
- Use of abortcontroller
- HTTP Caching Protocol practice
- NFT双币质押流动性挖矿dapp合约定制
猜你喜欢
![Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration](/img/9f/64b0b83211bd1c615f2db9273bb905.png)
Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration
![[digital signal processing] discrete time signal (analog signal, discrete time signal, digital signal | sampling leads to time discrete | quantization leads to amplitude discrete)](/img/80/28d53985d56d64ca721b26e846c667.jpg)
[digital signal processing] discrete time signal (analog signal, discrete time signal, digital signal | sampling leads to time discrete | quantization leads to amplitude discrete)

分布式Session解决方案

HTTP Caching Protocol practice

Centos8 PostgreSQL initialization error: initdb: error: invalid locale settings; check LANG and LC_* environment

带你认识图数据库性能和场景测试利器LDBC SNB

关于TensorFlow使用GPU加速

CentOS8-postgresql初始化时报错:initdb: error: invalid locale settings; check LANG and LC_* environment
The role of the symbol @ in MySQL

E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)
随机推荐
Jialichuang EDA professional edition all offline client release
2022-2-15 learning the imitated Niuke project - Section 5 shows comments
Openssf security plan: SBOM will drive software supply chain security
PSS: vous n'êtes qu'à deux niveaux du NMS Free + Lifting point | 2021 Paper
About fast exponentiation
PolarDB-X开源版有没有支持 mysql5.7 的版本?
Cannot determine value type from string ‘<p>1</p>‘
Design of UART controller based on FPGA (with code)
SIGKDD22|图“预训练、提示、微调”范式下的图神经网络泛化框架
Vulnerability recurrence ----- 34. Yapi remote command execution vulnerability
#28对象方法扩展
Indexeddb learning materials
如果想用dms来处理数据库权限问题,想问下账号只能用阿里云的ram账号吗(阿里云的rds)
Google tool splits by specified length
Does polardb-x currently not support self-made database service Das?
HTTP Caching Protocol practice
Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration
Basic configuration and usage of Jupiter notebook
Can the teacher tell me what the fixed income + products are mainly invested in?
【kotlin】第二天