current position：Home>Python from 0 to 1 (day 16) - Python conditional judgment 3
Python from 0 to 1 (day 16) - Python conditional judgment 3
2022-02-01 05:18:41 【My family】
This is my participation 11 The fourth of the yuegengwen challenge 21 God , Check out the activity details ：2021 One last more challenge
Last , Finally came to the code scanning payment stage , In fact, the development of code scanning payment is not plain sailing ,2014 year , Grab wechat red envelopes , Scan the red code to download the application and send a red envelope , Lay the user foundation for code scanning payment , Then the central bank stopped QR code payment , The reason is information security and capital security ,2016 Alipay launched red code to send red envelopes , QR code payment status recognized again , Market blowout , thereafter , The prospect of code scanning payment is bright .
From this we can see that , In code scanning payment, we are most concerned about security , So , WeChat and Alipay also provide various protective measures. , Common is the single limit , Frequent payment restrictions , Limit, etc , To better protect consumers' payment security .
In specific payment scenarios , The most common is the large amount authentication protection mechanism , The following payment scenarios
Payment limit rules 1. If the payment amount is greater than 500 1). If the payment password is correct , Successful payment 2). otherwise , Failure to pay 2. If the payment amount does not exceed 500 1). If small amount password free verification is enabled , Successful payment 2). If the small amount password free verification is not enabled 1). If the payment password is correct , Successful payment 2). If the payment password is wrong , Failure to pay Copy code
Like this, if there's a if at the bottom （ In other words, there are conditions in the conditions ） The situation of , How do we use Python Write down the rules above , And get the evaluation ？
The answer is —— Nested conditions .
if Nested application scenarios , The short answer is ： When the basic conditions are met , Add additional condition judgment under the basic condition .
Like the basic condition above is 500 element ,500 If the password is more than yuan, the payment will be successful only if the password is correct ,500 Check whether micro payment is enabled for less than RMB , If it's on , You can pay directly without entering a password , If it's not turned on , You need to enter the payment password and the password is correct to pay successfully .
therefore , The payment scenario is represented by code , Should use the if Nesting to complete the code :
# The correct password is 123456 # The password is assigned to 123456 password='123456' # The payment amount is assigned as 265 paymoney=265 # Secret free payment is enabled by default check=false # Conditions : If the payment amount is greater than 500 if paymoney > 500: # Conditions : If the password entered is 123456 if password=='123456': print(' Successful payment of large amount ') # Conditions : The password is not 123456 else: print(' Wrong password , Large amount payment failed ') # Conditions : The payment amount is less than 500 else: # Conditions : If secret free payment is enabled if check: print(' Small amount secret free payment succeeded ') # Conditions : If you don't open secret free payment else: # Conditions : If the password is correct if password == '123456': print(' Small amount payment succeeded ') # Conditions : If the password is wrong else: print(' Wrong password , Small amount payment failed ') Copy code
The result is that small payments are successful , Can you see the execution flow of the program ? Let's analyze the following
if Nested execution sequence
First , Let's start with an overview of , This code of payment verification rules , It is divided into four parts 【 assignment 、if、else、
print() 】, Two large code groups .
also , In code group 1 Of if Conditions and code groups 2 Of else Under the condition of , It also contains conditional judgment commands if...else..., In code group 2 Of course else Conditions include if...else.
that , How do we understand this nested command ？
Indent the same command at the same level , First step , The computer will execute commands one by one in order .
Then the execution order of the computer is :
How to write nested code
if Nesting involves multiple conditional judgments , And it's the judgment of the set of conditions , For clarity of logic , We can use “ From the outside to the inside , Divide and rule ” How to write if nesting .
There's a bunch of couriers , To be shipped separately to Beijing , Shanghai , A community in Guangzhou . So how do we classify express by address ?
First step , We classify express according to Beijing, Shanghai and Guangzhou .
The simulation code is like this .
address=' Beijing ' if address==' Beijing ': print(' Beijing Center ') elif address==' Shanghai ': print(' Shanghai Center ') elif address==' Guangzhou ': print(' Guangzhou Center ') else: print(' Invalid address ') Copy code
The second step , We classify the express arriving at the center of Beijing by area .
The simulation code is like this .
if address==' Beijing ': if district==' Changping District ': print(' Changping District Center ') elif district==' Shunyi District ': print(' Shunyi District Center ') elif district==' Tongzhou District ': print(' Tongzhou District Center ') else: print(' Other areas ') Copy code
That's when we find out , We just need to further improve the express delivery to Beijing . In fact, this is a programming idea , Is the idea of divide and rule , Divide the complex problem into several small problems to deal with , The problem becomes very simple .
The third step , Suppose the express arrives in Changping District , We can further pick up and practice by the street , Finally, distribute by cell .
Is nesting fun and practical , But let me remind you , Don't forget to indent .
Last, last , Let's summarize the knowledge points :
author[My family],Please bring the original link to reprint, thank you.
The sidebar is recommended
- Django paging (II)
- Concurrent. For Python concurrent programming Futures or multiprocessing?
- Programmers over the age of 25 can't know a few Chinese herbal medicines. Python crawler lessons 9-9
- Python crawler from introduction to pit full series of tutorials (detailed tutorial + various practical combat)
- The second bullet of class in Python
- Python object oriented programming 03: class inheritance and its derived terms
- How IOS developers learn Python Programming 13 - function 4
- Python crawler from introduction to mastery (VI) form and crawler login
- Python crawler from entry to mastery (V) challenges of dynamic web pages
- Deeply understand pandas to read excel, TXT, CSV files and other commands
guess what you like
Daily python, Chapter 18, class
"I just want to collect some plain photos in Python for machine learning," he said. "I believe you a ghost!"
Python implements filtering emoticons in text
When winter comes, python chooses a coat with temperament for mom! Otherwise, there's really no way to start!
Python crawler - get fund change information
Highlight actor using Python VTK
Python crawler actual combat: crawling southern weekend news articles
leetcode 406. Queue Reconstruction by Height（python）
leetcode 1043. Partition Array for Maximum Sum （python）
- Python * * packaging and unpacking details
- Python realizes weather query function
- Python from 0 to 1 (day 12) - Python data application 2 (STR function)
- Python from 0 to 1 (day 13) - Python data application 3
- Numpy common operations of Python data analysis series Chapter 8
- How to implement mockserver [Python version]
- Van * Python! Write an article and publish the script on multiple platforms
- Python data analysis - file reading
- Python data De duplication and missing value processing
- Python office automation - play with browser
- Python series tutorial 127 -- Reference vs copy
- Control flow in Python: break and continue
- Teach you how to extract tables in PDF with Python
- leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal（python）
- leetcode 1338. Reduce Array Size to The Half（python）
- Object oriented and exception handling in Python
- How to configure load balancing for Django service
- How to embed Python in go
- Python Matplotlib drawing graphics
- Python object-oriented programming 05: concluding summary of classes and objects
- Python from 0 to 1 (day 14) - Python conditional judgment 1
- Several very interesting modules in Python
- How IOS developers learn Python Programming 15 - object oriented programming 1
- Daily python, Chapter 20, exception handling
- Understand the basis of Python collaboration in a few minutes
- [centos7] how to install and use Python under Linux
- leetcode 1130. Minimum Cost Tree From Leaf Values（python）
- leetcode 1433. Check If a String Can Break Another String（python）
- Python Matplotlib drawing 3D graphics
- Talk about deep and shallow copying in Python
- Python crawler series - network requests
- Python thread 01 understanding thread
- Analysis of earthquake distribution in the past 10 years with Python~
- You need to master these before learning Python crawlers
- After the old friend (R & D post) was laid off, I wanted to join the snack bar. I collected some data in Python. It's more or less a intention
- Python uses redis
- Python crawler - ETF fund acquisition
- Detailed tutorial on Python operation Tencent object storage (COS)
- [Python] comparison of list, tuple, array and bidirectional queue methods
- Go Python 3 usage and pit Prevention Guide