current position:Home>Python from 0 to 1 (day 14) - Python conditional judgment 1

Python from 0 to 1 (day 14) - Python conditional judgment 1

2022-02-01 00:40:40 My family

This is my participation 11 The fourth of the yuegengwen challenge 19 God , Check out the activity details :2021 One last more challenge

When we listen to music with wireless headphones , We can set it to play the next song by tapping the headset twice . If I just set the headset twice , Just play the next one , that , Due to lack of headphones in use ( Earphones inserted into the ear are considered to be in use ) The premise of , Whether you are using , Just a little bit , Will play the next .

therefore , Our settings should be like this : Two taps , If the headset is in use , Just play the next one .

And if in this setting ... Just ... Logic , Is the logic of communicating with computers --- conditional , Its function is to clearly let the computer know : Under what conditions , What to do .

about Python It's the same ,Python The reason why automation tasks can be completed , such as Python The crawler project can successfully obtain the data information specified by us , Because it can perform conditional judgment .

Next , We passed one of China's four new inventions “ Sweep code payment ” Let's feel the fun of logical judgment .

QR code payment sounds like a very new technology , Actually , This is similar to the mobile newspaper , Not a novel technology . Back in the last century 90 years , QR code payment technology has been formed , But it has just sprung up in China in recent years .

2014 The wechat red envelope went online during the Spring Festival , It lays a user foundation for code scanning payment .


import random
print(" Please enter the red envelope amount , Two decimal places at most ")

money=round(float(input()),2)
print(" Please input the number of red packets ")
number=int(input())
print(" Please enter the red envelope method :1. Red envelopes  2. Ordinary red envelopes  3. Exclusive red envelope ")
mode=int(input())
money=int(money*100)
if mode == 1:
    for i in range(1, number):
        a = random.randint(1, money-(number-i))  #  Generate the amount of red packets randomly 
        money = money-a
        print(" The first " + str(i) + " personal , received " + str(a/100) + " element ,")
    print(" The first " + str(number) + " personal , received " + str(money/100) + " element ,")
elif mode == 2:
    for i in range(1, number+1):  #  Limit the number of people 
        print(" The first " + str(i) + " personal , received " + str(round((money/100)/number,2)) + " element ")
else:
    print(" The designated person obtains "+str(money/100)+" element ")
 Copy code 

Do you see ? This is conditional judgment if … Just … The magic of logic . that , This conditional judgment code , How to size it ?

conditional

stay Python The universe , There are three forms of conditional judgment statements , Let's start with the simplest one-way judgment :if Start talking about :

One way judgment :if

One way judgment :if What does it stand for ? We know that when sending red envelopes , If the exclusive red envelope exceeds 200 element , You will be prompted that the amount of a single red envelope cannot exceed 200 element .

You'll find that , In the example above , There is a if ... Just . that , We translate this passage in code , You can use it if To describe , below , Please run the following code .

#  For a single red envelope denomination 
money = 201

#  Conditions : If a single red envelope in exclusive mode exceeds 200 element 

if money > 200:

    #  result : Show ‘ The amount of a single red envelope cannot exceed 200 element ’ Result 

    print(' The amount of a single red envelope cannot exceed 200 element ')
 Copy code 

that , How is this code implemented ?

First , First line of code , Use the assignment operator = Assign a value to the current situation : Give the red envelope the denomination 201 Assign a value to a variable --- Red envelope denomination

The second step , determine if Conditions : If the variable money Value >200 when , Just run the colon , The next line of command .

The third step , use print() Command to print out the result : The amount of a single red envelope cannot exceed 200 element . therefore ,if One way judgment logic of statements , We can sum it up like this :

ad locum , You may have noticed a detail : A colon in a condition determination code : after 、 In front of the next line , It's going to be empty , But why ?

First , In the language of computer communication , The scientific name of a space is indent , For example, we need to fill two spaces when we write an article , This is called first line indentation . about Python for , Colons and indents are a syntax . It will help Python Differentiate between the layers of code , Understand the logic and sequence of condition execution .【 notes : The indent is four spaces or one Tab key 】

also , stay if In conditional language , Indentation does not require us to press the spacebar manually . When you type : Press enter , Our development tools ( Used for compiling Python Code program ) For the convenience of programming , Will automatically implement the next line of code , Right indent function .

here , Indented content (print() function ) and if Conditional statements make up a block of code ( all in one ), Become if Internal commands under conditions .

It means : If the assignment satisfies if Conditions , The computer will perform exactly if Commands inside conditions ( Indented blocks of code ).

Then below , Please run the following code , Look at the results of the run .

#  For a single red envelope denomination 
money = 201

#  Conditions : If a single red envelope in exclusive mode exceeds 200 element 

if money > 200:

    #  result : Show ‘ The amount of a single red envelope cannot exceed 200 element ’ Result 

print(' The amount of a single red envelope cannot exceed 200 element ')
 Copy code 

Execute the above code , Console prompt error :IndentationError: expected an indented block

Indentation error : Expect an indent block

This is because , When we remove the indentation ,if Conditions and print The command becomes two different code groups , It belongs to a parallel relationship . You see :if Under the following conditions , Missing actions to perform . Let's think about : Whether the conditions are true or not , Can't do anything , The existence of this condition makes no sense .

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

Random recommended