current position:Home>Python interface automation test framework (basic part) -- loop statement of process control for & while

Python interface automation test framework (basic part) -- loop statement of process control for & while

2022-01-31 06:49:03 I am the white moonlight you can't get

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

Preface

Very happy ,python Learned the loop statement , It's getting more and more fun , Learn here , Basically, it can also handle some pipeline work , It's just a little less decorative and not so flexible 、 Low reusability .

image.png

Loop statement

python There are... In the loop statement for and while; As shown in the figure above , Loops basically need to do something , Because you don't need all the data , I'll also bring if Conditionally control the output .

while loop

General sentence pattern

#  As a condition , by True Just execute the code block inside , by false Then exit the loop 
while (condition): 
    print(" Execute statement ")
 Copy code 

Hint :python There is no do...while loop

  • Seems to be python Most users , Not used while...else Such conditions
while False:
    print(" Will not be executed ")
else:
    print(" When while Condition is False Execution only ")
 Copy code 
  • Example : seek 1-100 The sum between , Just use while Loop through
dig=100

sum=0
count=1
while count <=dig:
    sum+=count
    count+=1
    
print("1 To {} The sum between is :{}".format(dig,sum))
#  Output results :1 To 100 The sum between is :5050
 Copy code 
  • Code analysis :

    • count As while Cycle counter , It cannot exceed the target value dig; Otherwise exit the loop , and sum+=count, Every time sum+count Then reassign to sum
  • Of course, this is the result of our study , How do you make sure the result is correct ?

    • Two ways , Do it yourself , What a joke ; Of course, find rules , remove 100 and 50, Add the head and tail to get 100 The number of 49 Yes , Such as :1+99、2+98、3+97, And so on , until 49+51, That is, there will be 49 Add the head and tail together to get 100, Plus 100+50, It's not 5050
    • The second method is naturally to use programs to verify ,python Provides sum()、range() function , It needs to be criticized , In the above example sum=0, That is, it will cover the original sum() function , So... Cannot be used in the current code block sum() Function to sum .
# sum() The argument to the function must be of iteratable type ,str With the exception of 
s=sum(range(1,101))

print(s) #  Output :5050

#  The built-in functions will be introduced later range function 
 Copy code 
  • while True: This loop has a special scenario , That is, the client needs real-time requests , Infinite loops are very useful .

for loop

This is a bit of a special loop , Because it is for...in The combination of ,in What is it ? Our members , Actually for Namely while, It's just condition Instead of x in xxx: nothing more , So it can't be for True But can x in xxx; Think about it :while Can you match x in xxx Well ?

  • for Loop traversal list Or sequence types such as strings
a="1234567"

for i in a:
    print(i)
#  Output :
1
2
3
4
5
6
7
 Copy code 
  • for Cycles often follow range() Functions are mixed together ;range() A function is a conditional function that is left closed and right open
for i in range(5):
    print(i)

#  Output :
0
2
3
4
 Copy code 

range() function

  • range() The default function is from 0 Start , The interval can be specified :range(1,5)
for i in range(1,5):
    print(i)

#  Output :
1
2
3
4
 Copy code 
  • Recall what I said before str、tuple、list Formula of type for slice value :(n:m:k), But in range It needs to be changed into a comma :range(n,m,k),m>n,k Step length
#  Odd number 
for i in range(1,10,2):
    print(i)

#  Output :
1
3
5
7
9
 Copy code 

continue and break

Literally , One is continue Continue to cycle , One break Is to interrupt the loop

  • For example while True: It's an infinite loop , If accompanied by if Under controlled conditions , When the conditions are met break Out of the loop
# cmd Get into python Interactive command mode 

>>> count=1
>>> while True:
...     print(count)
...     if count==4:
...         print(count)
...         break
...     count+=1
...
1
2
3
4
4
 Copy code 
  • If the above example is replaced by continue, Then infinite loop , It hasn't stopped yet , It ends itself .
  • Be careful 1: All from for or while The loop that jumps out will not execute the following else;
  • Be careful 2:continue and break If the position of the is in front of a piece of code , Then the code behind it will not be executed .

i=1
while i<10:
    if i==3:
        continue  
        #  Will not execute the following i+=1,i Forever 3, And then there's an infinite loop 
        print(" Unable to execute ")
    i+=1
 Copy code 
  • So think about It goes on , It's just continue Add conditions before :i+=1
# cmd Input python Go into interactive mode :

>>> i=1
>>> while i<10:
...     print(i)
...     if i==3:
...         i+=1
...         continue
...         print(" Statements that cannot be executed ")
...     print(" When i==3, This sentence does not execute ,{}".format(i))
...     i+=1
...
1
 When i==3, This sentence does not execute ,1
2
 When i==3, This sentence does not execute ,2
3
4
 When i==3, This sentence does not execute ,4
5
 When i==3, This sentence does not execute ,5
6
 When i==3, This sentence does not execute ,6
7
 When i==3, This sentence does not execute ,7
8
 When i==3, This sentence does not execute ,8
9
 When i==3, This sentence does not execute ,9

#  Did you miss i==3 The sentence of does not execute !!!
 Copy code 
  • break Keyword is also an example :
# cmd Input python Go into interactive mode 

>>> i=1
>>> while i<10:
...     print(i)
...     if i==3:
...         i+=1
...         break
...         print(" Statements that cannot be executed ")
...     i+=1
...     print(" Only until i==3")
... else:
...     print(" Jump out of the loop above , Will not execute the code block here ")
...
1
 Only until i==3
2
 Only until i==3
3
 Copy code 

pass keyword

It's an empty statement , Do nothing , It's space occupying ; Maintain structural integrity .


if i in range(4):
    pass

#  Doing nothing , But it will traverse 
 Copy code 

Think about it :while Can you match x in xxx Well ?

copyright notice
author[I am the white moonlight you can't get],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310649023677.html

Random recommended