(7) while loop of process control

Conditional loop: while, the syntax is as follows

while condition:
# loop body

# 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
# 如果条件为假,那么循环体不执行,循环终止
print 0-10
count=0
while count<=10:
    print(count)
    count=count+1
print even numbers
count=0
while count <=10:
    if count%2==0:
        print(count)
count=count+1


==3 infinite loop

import time
num=0
while True:
print('count',num)
time.sleep(1)
num+=1 ==

Loop nesting and tags
tag=True 

  while tag:

    ......

    while tag:

      ........

      while tag:

        tag=False

For the exercise, the requirements are as follows:
1. Validate the user name and password entered by the user in a loop.
2. After the authentication is passed, the running user executes the command repeatedly.
3. When the user's input command is quit, exit the entire program.

 name="bruce"
 
 password="123"
 
 while True:
 
    inp_name=input('username>>>')
    
    inp_pswd=input('password>>>')
    

    if inp_name==name and inp_pswd==password:
        while True:
            cmd=input('>>>:')
            if not cmd:continue
            if cmd=="quit":
                break

                print("run <%s>" %cmd )
            else:
                print("incorrect username or password")
                continue
                break


why

    if not cmd:continue

    if cmd=="quit"

Can't put the if not below, why is there a colon instead of an equal sign


        
     
        
while+else

Unlike other languages, else is generally only matched with if. In Python, there is also a while...else statement. The effect of the else after while means that when the while loop is executed normally, if it is not interrupted by break in the middle, it will be executed after the else. statement

number_1=0
while number_1<=5:
    number_1 += 1
    if number_1==3:
        break

    print(number_1)

else:print('finished')

1
2

 #1. Use while loop to output 1 2 3 4 5 6 8 9 10

 count_1=1
while count_1 <= 10:
    if count_1 == 7:
        count_1 += 1
        continue
    print(count_1)
    count_1+=1
    
    
    
    
    count_2=1
while count_2<=10:
    if count_2!=7:
        print(count_2)
    count_2+=1

Find the sum of all numbers from 1 to 100

plus_1=0
plus_2=plus_1+1
while plus_1<=100:
    plus_2=plus_1+plus_2
    plus_1+=1
print(plus_2)

Find the value of 1-2+3-4+5...99

res=0
count_4=1
while count_4<=100:
    if count_4%2==0:
        res=res-count_4
    else:
        res=res+count_4
    count_4+=1
print(res)

. User login (three chances to retry) .

retry=3
u_='bruce'
p_='123'
while retry :
    username_= input('input your username>>: ')
    password_=input('input your password>>>: ')
    if username_==u_ and password_==p_:
        print("login success")
        break
    else:
        retry -= 1
        print('login failed, {} times left' .format (retry))

continue to the end?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324487048&siteId=291194637
Recommended