当前位置:网站首页>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/
边栏推荐
- 利用Redis实现订单30分钟自动取消
- E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)
- 是不是只要支持JDBC / ODBC协议的客户端恐惧,PolarDB-X可通过相关工具的客户端访问?
- 2022-2-16 learning the imitated Niuke project - Section 6 adding comments
- 2022-2-15 learning the imitated Niuke project - Section 5 shows comments
- Knightctf 2022 web section
- What is the open source compatibility of the current version of polardb-x? mysql8?
- 专用发票和普通发票的区别
- CAS comparison and exchange
- sql注入原理
猜你喜欢
![Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration](/img/9f/64b0b83211bd1c615f2db9273bb905.png)
Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration
![洛谷_P1008 [NOIP1998 普及组] 三连击_枚举](/img/9f/64b0b83211bd1c615f2db9273bb905.png)
洛谷_P1008 [NOIP1998 普及组] 三连击_枚举

漏洞复现----34、yapi 远程命令执行漏洞

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊

Keep valid digits; Keep n digits after the decimal point;

E ModuleNotFoundError: No module named ‘psycopg2‘(已解决)
![Beginner level Luogu 1 [sequence structure] problem list solution](/img/60/5e151ba31eb00374c73be52e3bfa7e.png)
Beginner level Luogu 1 [sequence structure] problem list solution

The latest development course of grain college in 2022: 8 - foreground login function

Centos8 PostgreSQL initialization error: initdb: error: invalid locale settings; check LANG and LC_* environment
MySQL中符号@的作用
随机推荐
Design of digital video signal processor based on FPGA (with main code)
About tensorflow using GPU acceleration
Problems encountered in vs compilation
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
避孕套巨头过去两年销量下降40% ,下降原因是什么?
一场分销裂变活动,不止是发发朋友圈这么简单!
Gin general logging Middleware
I want to buy fixed income + products, but I don't know what its main investment is. Does anyone know?
Basic configuration and usage of Jupiter notebook
[kotlin] the next day
Volatile and JMM
[170] the PostgreSQL 10 field type is changed from string to integer, and the error column cannot be cast automatically to type integer is reported
一场分销裂变活动,不止是发发朋友圈这么简单!
手机号码的格式
Numerical extension of 27es6
可变参数模板 Variadic Templates
What is the open source compatibility of the current version of polardb-x? mysql8?
Piblup test report 1- pedigree based animal model
Markdown syntax
[interview questions] common interview questions (I)