8.1 day06

while loop

Repeat doing one thing, called cycle

age = 16

while True:
    inp_age = input('age:')
    inp_age = int(inp_age)

    if inp_age > age:
        print('猜大了')
    elif inp_age < age:
        print('猜小了')
    else:
        print('猜对了')

print('hello')

The above section of code used and the while loop determines if

Be run after the discovery, the age when you enter and get through if the statement cycle, the system will run again while inside the code, an infinite loop

Some people may ask: So I should not lose the right to stop the cycle it? while True is True and what does it mean? Why hello rearmost has not been running it?

These questions I will be elaborated on in the next

while syntax

while <条件>:
    <需要重复执行的代码块>  
print("恭喜你跳出循环啦")   

The above is the most simple while loop

When the condition is satisfied (i.e., the condition is true, such as 1 == 1) runs, finished block will end determination condition again until the condition is not satisfied, operation will end, the next block of code execution.

So some people may ask: So you still did not solve the problem guessed it would not run Well, you're not blind in this BB to waste my time? Then the next I'll talk about the usage statement while in break and continue

while + break grammar

break

When the while loop, if the encounter break, this cycle will be terminated, so plus break in conditions guessed's, when only input 16, will encounter break, out of the loop, contrary to any other input value, would have been the cycle continues.

age = 16

while True:
    print(1)  # 1
    inp_age = input('age:')  # '16'
    inp_age = int(inp_age)  # 16

    if inp_age > age:  #
        print('猜大了')
    elif inp_age < age:  #
        print('猜小了')
    else:
        print('猜对了')
        break  # 不运行下面的代码,并且跳出循环
        print("你不可能能看到我")
print("恭喜你跳出循环啦")

If the input 16 of FIG words, the line of code following the break does not occur, directly out of the loop to the rearmost line of source code

Is not it simple?

while + continue

count = 0

while True:
    count += 1

    if count == 5:  #
        continue # 继续,跳出本次循环,不运行下面的代码,直接开始下一次循环

    if count == 11:
        break

    print(count)

Direct encounters continue to start the next cycle, continue following code will not be executed

Therefore, the results of the above code is: 1234678910

When the count == 5 Shi, continue statement is executed, directly into the next cycle, the count is not printed

The count == 11 Shi, break statement is executed, direct the end of the cycle, count naturally will not be printed

while + else (as understood only)

When the cycle is not break, else it will be triggered

(Else rarely use)

# 代码一
count = 0

while count < 10:
    count += 1

    if count == 5:
        break  # break除了干掉while循环,还会干掉else

    print(count)
else:
    print("当while循环没有被break的时候我会触发,但尽量不要使用我")
    
    
# 代码二    
ount = 0

while count < 10:
    count += 1

    if count == 4008823823:
        break  # break除了干掉while循环,还会干掉else

    print(count)
else:
    print("当while循环没有被break的时候我会触发,但尽量不要使用我")    
    

Will find the first piece of code is executed after running the break statement, else statement does not trigger

The second paragraph of the code break statement will not be executed, else it is triggered

由此可见,while 和else 在break 面前都是弟弟,完全不是对手

三者区别

break:跳出本层循环,跳出循环

continue:跳出本次循环

else:break的“亲弟弟“,有break就没他什么事

可以由一段简单的话来充分理解这三者的区别及关系:

如果你每天0点起来25点睡觉,连续30天,如果中途没有被干掉,奖励你一本书 <通往天堂的捷径>

break: 当你第十天的时候被干掉了了,剩下的20天全部减免了,当然你就上不了天堂了

continue:当你第十天的时候被干掉了,第十天减免了,但是你充钱复活了,从第十一天开始继续

else:当你中途没有被break干掉,一直在修仙,就会得到 <通往天堂的捷径>

接下来,我们可以根据while循环的用法,来实现一段功能较为完整的登录系统:

count = 0
username_db = 'hyc'
pwd_db = '123456'


code = random.randint(1000,9999)   #
code = f'{code}{chr(random.randint(97,117))}{chr(random.randint(97,117))}{chr(random.randint(97,117))}'


while count < 3:
    print(code)
    username = input('username:')
    pwd = input('pwd:')
    code_inp  = input('code:')

    if username_db == username and pwd_db == pwd and code == code_inp:
        print('登录成功,但是并没有什么卵用')
        break
    else:
        count += 1
        print('\033[43m傻逼,账号密码都忘了.验证码都看不懂\033[0m')

for循环

循环干一件事,循环容器数据类型

首先如果我们有一个列表,那么用while循环来遍历其中的值,该怎么做呢?

list = ['111','222','333']
# while循环遍历
count = 0
while True:
    if count == 0:
        break
    print(lis[count])
    count += 1

虽然我们成功了,但是是否觉得语句略显繁琐呢?

这时我们就可以使用for循环了

fori in list
    print(i)

这和while循环相比,是不是瞬间就简单很多了呢

同样的,for也有break,continue,else三种语法,因为和while差不多我就不在这多BB了,直接贴极端代码就可以看懂了

不过在看这几段代码前,首先要解释一下range函数的用法

print(list(range(3)))  # 0,1,2
print(list(range(10)))  # 0,1,2..9
print(list(range(2,5)))  # 2,3,4  # 顾头不顾尾
print(list(range(3,6)))  # 3,4,5  # 顾头不顾尾
print(list(range(3,7,3)))  # 3,6  # 顾头不顾尾
 print(list(range(1,10,2)))  # 1,3,5,7,9  # 顾头不顾尾

上面几段代码打完,range的用法其实已经很明了了

接下来再贴几段for循环的代码

# for + break
for i in range(1,11): # 1,2,3,4,5
    print(i)
    if i == 5:
        break  #跳出循环
# for + continue
for i in range(1,11): # 1,2,3,4,6,7,8,9,10
    if i == 5:
        continue  #跳出本次循环

    print(i)
# for + else

for i in range(1,11): # 1,2,3,4,6,7,8,9,10
    if i == 11:
        break  
    print(i)
else:  # 没有被break会触发
    print('你居然运行到了else')

for循环和while循环的区别

while

在某些情况下,while还是经常被用到,比如上面的登录大多都用while,但是它的缺点也很明显

1、会进入死循环,不可控,尽量少用

2、世间万物都可以作为循环的对象

for

for在某些情况(比如列表和字典)会比while简单得多,并且不会进入死循环,但也不是万能的

1、不会进入死循环,可控,尽量使用

2、只对容器类数据类型循环(可迭代对象)

Guess you like

Origin www.cnblogs.com/hyc123/p/11283412.html