<jupyter_start><jupyter_text>循环- 循环是一种控制语句块重复执行的结构- while  适用于广度遍历- for    开发中经常使用  while 循环- 当一个条件保持真的时候while循环重复执行语句- while 循环一定要有结束条件，否则很容易进入死循环- while 循环的语法是：    while loop-contunuation-conndition:        Statement<jupyter_code>import os
while 1:
    os.system('你真是一个小机灵鬼')<jupyter_output><empty_output><jupyter_text>示例：sum = 0i = 1while i <10:    sum = sum + i    i = i + 1<jupyter_code>sum_ = 0
i = 1
while i <10:
    sum_ = sum_ + i
    i = i + 1
    print(sum_)<jupyter_output>1
3
6
10
15
21
28
36
45<jupyter_text>错误示例：sum = 0i = 1while i <10:    sum = sum + ii = i + 1- 一旦进入死循环可按 Ctrl + c 停止  EP：![](../Photo/143.png)![](../Photo/144.png)  验证码- 随机产生四个字母的验证码，如果正确，输出验证码正确。如果错误，产生新的验证码，用户重新输入。- 验证码只能输入三次，如果三次都错，返回“别爬了，我们小网站没什么好爬的”- 密码登录，如果三次错误，账号被锁定<jupyter_code>import random
i = 0
while i < 3:
    yan = str(random.randint(1000,9999))
    print('验证码： ',yan)
    input_ = input('>>')
    if yan == input_:
        print('正确')
        break
    else:
        print('再输一次')
        i +=1
else:
    print('验证码次数超限')
import random
i = 0
while i <3 :
    yanzhengma1 = chr(random.randint(97,122))
    yanzhengma2 = chr(random.randint(97,122))
    yanzhengma3 = chr(random.randint(97,122))
    yanzhengma4 = chr(random.randint(97,122))
    yanzhengma = yanzhengma1 + yanzhengma2+ yanzhengma3 + yanzhengma4
    
    print('验证码是:',yanzhengma)
    input_ = input('>>')
    if yanzhengma == input_:
        print('验证码正确')
        break
    else:
        print('请再输入')
        i +=1
#     if i ==3:
#         print('验证码次数超限')
else:
    print('验证码次数超限')
import random
i = 0
while i <3 :
    list_ = ['1','2','3','4','5','6','7','8','9','0','q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','n','m']
    random.shuffle(list_)
    yanzhengma = ''.join(list_[0:4])
    print('验证码是:',yanzhengma)
    input_ = input('>>')
    
    if yanzhengma == input_:
        print('验证码正确')
        break
    else:
        print('请再输入')
        i +=1
#     if i ==3:
#         print('验证码次数超限')
else:
    print('验证码次数超限')<jupyter_output>验证码是: qmiy
>>qmiy
验证码正确<jupyter_text>尝试死循环  实例研究：猜数字- 你将要编写一个能够随机生成一个0到10之间的且包括两者的数字程序，这个程序- 提示用户连续地输入数字直到正确，且提示用户输入的数字是过高还是过低  使用哨兵值来控制循环- 哨兵值来表明输入的结束- ![](../Photo/54.png)  警告![](../Photo/55.png)  for 循环- Python的for 循环通过一个序列中的每个值来进行迭代- range(a,b,k)， a，b，k 必须为整数- a: start- b: end- k: step- 注意for 是循环一切可迭代对象，而不是只能使用range<jupyter_code>for iterm in range(0,10,2):
    print(iterm)
for i in {'1':1,'2':2}:
    print(i)<jupyter_output>1
2<jupyter_text>在Python里面一切皆对象  EP:- ![](../Photo/145.png)<jupyter_code>i = 0
str_ = 'awqrqerwt'
while i<8:
    print(str_[i])
    i +=1
sum_ = 0
i = 0
while i<1001:
    sum_ = sum_ +i
    i +=1
print(sum_)
sum_ = 0
i = 0
for i in range(1,10000):
    sum_ +=i
    if sum_ >10000:
        break
print(sum_)<jupyter_output>10011<jupyter_text>嵌套循环- 一个循环可以嵌套另一个循环- 每次循环外层时，内层循环都会被刷新重新完成循环- 也就是说，大循环执行一次，小循环会全部执行一次- 注意：> - 多层循环非常耗时  - 最多使用3层循环<jupyter_code>for i in range(2):
    for j in range(8):
        print(i,j)<jupyter_output>0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
1 0
1 1
1 2
1 3
1 4
1 5
1 6
1 7<jupyter_text>EP：- 使用多层循环完成9X9乘法表- 显示50以内所有的素数<jupyter_code>for i in range(1,10):
    for j in range(1,i+1):
        print('{}*{}={}'.format(j,i,j*i),end="  ")
    print()
list_=[]
i=2
for i in range (2,100):
    j=2
    for j in range(2,i):
        if(i%j==0):
            break
    else:
        list_.append(i)
print(str(list_))
for i in range(2,50):
    for j in range(2,i):
        if i % j == 0 :
            break
    else :
        print(i)<jupyter_output>2
3
5
7
11
13
17
19
23
29
31
37
41
43
47<jupyter_text>关键字 break 和 continue- break 跳出循环，终止循环- continue 跳出此次循环，继续执行  注意![](../Photo/56.png)![](../Photo/57.png)  Homework- 1 ![](../Photo/58.png)<jupyter_code>i = 0
n = 0
m = 0
sum1 = 0
while True:
    num = eval(input('Enter an integer,the input ends if it is 0:'))
    if num > 0:
        n +=1
    elif num < 0:
        m +=1
    else:
        print(sum1 / i)
        break
    i +=1
    sum1 = sum1 + num
print('正数有：'+str(n)+'个')
print('负数有：'+str(m)+'个')<jupyter_output>Enter an integer,the input ends if it is 0:1
Enter an integer,the input ends if it is 0:2
Enter an integer,the input ends if it is 0:-1
Enter an integer,the input ends if it is 0:3
Enter an integer,the input ends if it is 0:0
1.25
正数有：3个
负数有：1个<jupyter_text>- 2![](../Photo/59.png)<jupyter_code>num = 10000
sum_ = 0
for i in range(10):
    num = num + num*0.05
    if i < 10:
        sum_ = num + sum_
#         print(sum_)
#     print(num)
print(str(i+1)+'年后大学四年总学费为：'+str(sum_)+'美元')<jupyter_output>10年后大学四年总学费为：132067.87162326268美元<jupyter_text>- 3![](../Photo/58.png) - 4![](../Photo/60.png)<jupyter_code>count = 0
for i in range(100,1000):
    if (i%5 == 0) and (i%6 == 0):
        print(i,end=" ")
        count +=1
        if count%10 ==0:
            print()<jupyter_output>120 150 180 210 240 270 300 330 360 390 
420 450 480 510 540 570 600 630 660 690 
720 750 780 810 840 870 900 930 960 990<jupyter_text>- 5![](../Photo/61.png)<jupyter_code>i = 1
while i > 0:
    if i**2 > 12000:
        print(i)
        break
    i +=1
i = 1
while i > 0:
    if i**3 < 12000 and (i+1)**3 > 12000:
        print(i)
        break
    i +=1<jupyter_output>22<jupyter_text>- 6![](../Photo/62.png) - 7![](../Photo/63.png) - 8![](../Photo/64.png)<jupyter_code>sum_ = 0
for i in range(1,98,2):
    n = i/(i+2)
    sum_ = sum_ + n
print(sum_)<jupyter_output>45.124450303050196<jupyter_text>- 9![](../Photo/65.png)<jupyter_code>for iterm in range(1,10000,2):
    pi = 4*(1-(1/iterm))<jupyter_output><empty_output><jupyter_text>- 10 ![](../Photo/66.png)<jupyter_code>sum_= 0
for i in range(1,10000):
    for j in range(1,i)
        if i%j == 0
        print(i)
for i in range(1,10000):
    s = 0
    for k in range(1,i):
        if i%k==0:
            s = s + k
    if i == s:
        print(i)<jupyter_output>6
28
496
8128<jupyter_text>- 11![](../Photo/67.png)<jupyter_code>count = 0
for i in range(1,8):
    for j in range(1,i):
        print(j,i)
        count +=1
print('The total number of all combinations is : '+str(count))<jupyter_output>1 2
1 3
2 3
1 4
2 4
3 4
1 5
2 5
3 5
4 5
1 6
2 6
3 6
4 6
5 6
1 7
2 7
3 7
4 7
5 7
6 7
The total number of all combinations is : 21
{'fraction_non_alphanumeric': 0.17035966602440591, 'fraction_numerical': 0.09168272318561337, 'mean_word_length': 2.893125, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '648993', 'license': 'Apache-2.0', 'n_tokens_mistral': 3434, 'n_tokens_neox': 3257, 'n_words': 655, 'path': '9.13.ipynb', 'repo_name': 'xiayangaa/test', 'size': '6228'}