while loop example

Example 1: Input while loop 1234568910

count = 0
while count < 10:
    count += 1  # count = count + 1
    if count == 7:
        print(' ')
    else:
        print(count)
method one
count = 0
while count < 10:
    count += 1  # count = count + 1
    if count == 7:
        continue
    print(count)
Method Two

Example 2: The outputs of all the odd-numbered 1-100

count = 1
while count < 101:
    print(count)
    count += 2
method one
count = 1
while count < 101:
    if count % 2 == 1:
        print(count)
    count += 1
Method Two

Example 3: Find the number of all + 1-2 + 3-4 and 5 ... 99

sum = 0
count = 1
while count < 100:
    if count % 2 == 0:
        sum = sum - count
    else:
        sum = sum + count
    count += 1
print(sum)
Code

Example 4: Use the user login (unsuccessful attempts to enter the opportunity) and each output error is displayed when the remaining number of errors (Hint: Use string formatting)

username = "yangxiaoer"
password = "123456"
i = 3
while i > 0:
    zh = the INPUT ( " Please enter your login: " )
    i -= 1
    if zh == username:
        mm = the INPUT ( " Please enter your password: " )
         IF mm == password:
             Print ( " verification is successful landing ....... " )
             Print ( '' ' Congratulations on your successful landing!
            Users are welcome to enter
            Username:% s
            Password:% s
            '' ' % (Zh, mm))
             BREAK 
        the else :
             IF i == 0:
                 Print ( " you did not have the chance to see the next game over!! " )
                answer = input('再试试?Y or N')
                if answer == 'Y':
                    i = 3
             Print ( " Wrong password, please re-enter " )
             Print ( " you have " + str (i) + " chance " )
     the else :
         Print ( " ! Please enter the correct user name " )
         IF i == 0:
             Print ( " you did not have the chance! " )
            answer = input('再试试?Y or N')
            if answer == 'Y':
                i = 3
         Print ( " you have " + str (i) + " chance " )
 the else :
     Print ( ' TM you want to shameless ' )
Code

while else use cases

count = 0
while count <= 5 :
    count += 1
    if count == 3:break
    print("Loop",count)

the else :
     Print ( " loop done for normal execution " )
 Print ( " ----- ------ OUT of the while Loop " )
Code

 Bird Reference Reference Tutorial: https://www.runoob.com/python/python-while-loop.html

Guess you like

Origin www.cnblogs.com/qi1113/p/12349587.html