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

if nesting

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 ifelseprint() 】, 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 :

copyright notice
author[My family],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010518397704.html

Random recommended