While controlling the flow through the bad

While controlling the flow through the bad

First, grammar

Cycle is an iterative process, we need to repeat doing a live person, who would not be exhausted ah, but still so ... .. of boring, I want to xiaonianqing so, do not show in Sao gas all right? That Qibushibai white head wandering for so many years, so I say to the rational use of tools and resources, then you have to mention this man a big heavy guy, that is the legendary magic - computer, use the size of the child to do some repeat live it not flattered. A example: money, get a lot of play money, it would be able to blocked, so that the name has long been a legend of the TM machine. For example TM verification fails, the computer will enable us to once again enter the password. This time I have to tell us wile loops, while loops, also known as a conditional loop.

# 基本格式
while 条件
    code 1
    code 2
    code 3
    ...
eg(死循环):
while True:
    print('*1'*100)
    print('*2'*100)

eg:

# 实现ATM的输入密码重新输入的功能
while True:
    user_db = 'randy'
    pwd_db = '123'

    inp_user = input('username: ')
    inp_pwd = input('password: ')
    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
    else:
        print('username or password error')

result:

username: randy
password: 123
login successful

Although the above code to achieve the function, but the user password is entered right, he will continue to enter.

二、while +break

break off the circulation means terminates the current layer, the code will not be executed later, other code execution cycle outside.

eg:

while True:
    print('1')
    print('2')
    break
    print('3')

result:

1
2

break the code above is meaningless, the purpose is to make computer recycling and everyone else working cycle to deal with things, and he directly print after 1 and 2 exit the cycle. And we show a combination of lower interest while + break the code.

while True:
    user_db = 'randy'
    pwd_db = '123456'

    inp_user = input('username: ')
    inp_pwd = input('password: ')
    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
        break
    else:
        print('username or password error')

print('退出了while循环')

result:

username: Randy
password: 123456
the Login successful
landing success will exit the loop

三、while+continue

means continue to terminate this cycle, the next cycle directly into

n = 1
while n < 10:
    if n == 8:
        # n += 1  # 如果注释这一行,则会进入死循环
        continue
    print(n)
    n += 1

continue the code can not be added to the final step in the execution of the loop because the code to add the phrase is meaningless, as the following location continue where is meaningless. Note that the code to perform the last step, not the last line.

Fourth, the nested loop

ATM password success also requires a series of command operations, such as withdrawals, such as transfers. And after performing the operation function will exit the command function, i.e. the output to perform an input function q exit while loop function and exits the program ATM.

# 退出双层循环的while循环嵌套
while True:
    user_db = 'randy'
    pwd_db = '123456'

    inp_user = input('username: ')
    inp_pwd = input('password: ')

    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')

        while True:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                break
            print(f'{cmd} 功能执行')
        break
    else:
        print('username or password error')

print('退出了while循环')

username: Randy
password: 123456
the Login successful
enter the command you need: q
out of the while loop

六、while+else

while + else: else will only execute code in else in the while not break (almost no scenario).

 while+else
n = 1
while n < 3:
    print(n)
    n += 1
else:
    print('else会在while没有被break时才会执行else中的代码')

1
2
else will only execute code in else in the while not break

Guess you like

Origin www.cnblogs.com/randysun/p/11284679.html