<jupyter_start><jupyter_text>循环- 循环是一种控制语句块重复执行的结构- while  适用于广度遍历- for    开发中经常使用  while 循环- 当一个条件保持真的时候while循环重复执行语句- while 循环一定要有结束条件，否则很容易进入死循环- while 循环的语法是：    while loop-contunuation-conndition:        Statement  示例：sum = 0i = 1while i <10:    sum = sum + i    i = i + 1  错误示例：sum = 0i = 1while i <10:    sum = sum + ii = i + 1- 一旦进入死循环可按 Ctrl + c 停止<jupyter_code>i = 0
while i < 10:
    print("今天天气好晴朗")
    i += 1<jupyter_output>今天天气好晴朗
今天天气好晴朗
今天天气好晴朗
今天天气好晴朗
今天天气好晴朗
今天天气好晴朗
今天天气好晴朗
今天天气好晴朗
今天天气好晴朗
今天天气好晴朗<jupyter_text>EP：![](../Photo/143.png)![](../Photo/144.png)  验证码- 随机产生四个字母的验证码，如果正确，输出验证码正确。如果错误，产生新的验证码，用户重新输入。- 验证码只能输入三次，如果三次都错，返回“别爬了，我们小网站没什么好爬的”- 密码登录，如果三次错误，账号被锁定<jupyter_code>code = 'kimj'
suiji = str(input('>>'))
if suiji == code:
    print('验证码正确')
else:
    print('错误，请重新输入')
import random
#数字
while 1:
    number = random.randint(1000,9999)
    print('验证码为：',number)
    me = eval(input('>>'))

    if number == me:
        print('验证码输入正确')
        break
    else:
        print('验证码输入错误')
while True:
    n1 = chr(random.randint(97,122))
    n2 = chr(random.randint(97,122))
    n3 = chr(random.randint(97,122))
    n4 = chr(random.randint(97,122))
    N = n1 + n2 + n3 + n4
    print('验证码是：',N)
    me = input('输入验证码')
    if me == N:
        print('验证码正确')
        break
    else:
        print('验证码错误')
import random
a = [1,2,3,4,5,6]
random.shuffle(a)
a
import shuffle
import random
list = [20,16,10,5];
random.shuffle(list)
print('>>',list)
import random
random.shuffle(list)
while True:
    n1 = chr(random.randint(65,90))
    n2 = chr(random.randint(97,122))
    n3 = chr(random.randint(65,90))
    n4 = chr(random.randint(97,122))
    shuffle_n = [1,2,3,4,5,6]
    random.shuffle(shuffle_n)
    N = "".join(shuffle-n)
    print('验证码是：',N)
    me = input('输入验证码')
    if me.lower() == N.lower():
        print('验证码正确')
        break
    else:
        print('验证码错误')
import random
while True:
    choose = random.randint(0,1)
    list_ = [[65,90],[97,122]]
    range_ = list_[choose]
    N = ""
    for i in range(4):
        print(i)
        N += chr(random.randint(range_[0],range_[1]))
        print('验证码是：',N)
        me = input('输入验证码：')
        if me.lower() == N.lower():
            print('验证码正确')
            break
        else:
            print('验证码错误')
a = 'j'
a.upper()
a = 'J'
a.lower()<jupyter_output><empty_output><jupyter_text>尝试死循环  实例研究：猜数字- 你将要编写一个能够随机生成一个0到10之间的且包括两者的数字程序，这个程序- 提示用户连续地输入数字直到正确，且提示用户输入的数字是过高还是过低<jupyter_code>import random
while 1:
    N1 = random.randint(0,10)
    N2 = random.randint(0,10)
    input_ = eval(input('Sum'))
    if N1 + N2 == input_:
        print('Correct!!')
        break<jupyter_output><empty_output><jupyter_text>使用哨兵值来控制循环- 哨兵值来表明输入的结束- ![](../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 i in range(10,0,-1):
    print(i)
for i in 100:
    pass
range(10).__iter__()
a = []
a.__iter__
b = 'ab'
b.__iter__
c = {'a':'b'}
c.__iter__<jupyter_output><empty_output><jupyter_text>在Python里面一切皆对象  EP:- ![](../Photo/145.png)<jupyter_code>i = 1
sum_ = 0
while sum_ < 10000:
    sum_ += i
    i += 1
print(i)
print(sum_)
sum_ = 0
for i in range(1,10000):
    sum_ += i
    if sum_ > 10000:
        break
print(sum_)
print(i)<jupyter_output>10011
141<jupyter_text>嵌套循环- 一个循环可以嵌套另一个循环- 每次循环外层时，内层循环都会被刷新重新完成循环- 也就是说，大循环执行一次，小循环会全部执行一次- 注意：> - 多层循环非常耗时  - 最多使用3层循环  EP：- 使用多层循环完成9X9乘法表- 显示50以内所有的素数<jupyter_code>1x1 = 1
1x2 = 2,2x2 = 4
1x3 = 3,2x3 = 6,3x3 = 9
for i in range(1,10):
    for j in range(1,i + 1):
        print('%dx%d=%d'%(j,i,i*j),end=" ")
    print()<jupyter_output>1x1=1 
1x2=2 2x2=4 
1x3=3 2x3=6 3x3=9 
1x4=4 2x4=8 3x4=12 4x4=16 
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81<jupyter_text>关键字 break 和 continue- break 跳出循环，终止循环- continue 跳出此次循环，继续执行<jupyter_code>for i in range(2,50):
    if i == 2:
        print(i,'是素数')
    else:
        pass
for i in range(1,50):
    for j in range(2,i):
        if i % j == 0:
            break
    else:
        print(i)<jupyter_output>1
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47<jupyter_text>注意![](../Photo/56.png)![](../Photo/57.png)  Homework- 1 ![](../Photo/58.png) - 2![](../Photo/59.png)<jupyter_code>a = eval(input('输入今年的学费：'))
b = 1 + 0.05
c = a * b
d = c * b
e = d * b
f = e * b
g = f * b
h = g * b
i = h * b
j = i * b
k = j * b
l = k * b
print('第十年是',l,'美元')
sum_ = c+d+e+f+g+h+i+j+k+l
print('十年的总学费是',sum_,'美元')<jupyter_output>输入今年的学费：10000
第十年是 16288.946267774418 美元
十年的总学费是 132067.8716232627 美元<jupyter_text>- 4![](../Photo/60.png)<jupyter_code>for i in range(99,1001):
    if i%30==0:
        print(i,end = " ")<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>n=1
while n*n <12000:
    n=n+1
    continue
print(n)
n=1
while n*n*n<12000:
    n=n+1
    continue
print((n))<jupyter_output>23<jupyter_text>- 6![](../Photo/62.png)<jupyter_code>189.28 * 12. * 5.<jupyter_output><empty_output><jupyter_text>- 7![](../Photo/63.png)<jupyter_code>i = 1
a = i
while i < 50000:
    i += 1
    a += 1/i
print(a)<jupyter_output>11.397003949278504<jupyter_text>- 8![](../Photo/64.png)<jupyter_code>sum = 0
for i in range(3,100,2):
    sum += (i-2)/i
print(sum)<jupyter_output>45.124450303050196<jupyter_text>- 9![](../Photo/65.png)<jupyter_code>import math
for i in range(10000,100001,10000):
    a = 0
    for j in range(i,0,-1):
        a+=((-1)**(j+1))/(2*j-1)
    print(4*a)<jupyter_output>3.1414926535900434
3.1415426535898248
3.1415593202564693
3.141567653589797
3.1415726535897956
3.1415759869231277
3.1415783678755087
3.141580153589794
3.141581542478683
3.1415826535897935<jupyter_text>- 10 ![](../Photo/66.png)<jupyter_code>for i in range(1,1000):
    sum_ = 0
    for a in range(1,i):
        if i%a == 0:
            sum_ += a
    if sum_ == i:
        print(i)<jupyter_output>6
28
496<jupyter_text>- 11![](../Photo/67.png)<jupyter_code>list_=[]
for i in range(1,8):
    for j in range(1,8):
        if i != j and sorted([i,j]) not in list_:
            list_.append([i,j])
    print(list_)
    print(len(list_))<jupyter_output>[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7]]
6
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7]]
11
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7]]
15
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7], [4, 5], [4, 6], [4, 7]]
18
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7], [4, 5], [4, 6], [4, 7], [5, 6], [5, 7]]
20
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7], [4, 5], [4, 6], [4, 7], [5, 6], [5, 7], [6, 7]]
21
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7], [4, 5], [4, 6], [4, 7], [5, 6], [5, 7], [6, 7]]
21<jupyter_text>- 12![](../Photo/68.png)<jupyter_code>a = eval(input('输入10个数：'))
b = eval(input('输入10个数：'))
c = eval(input('输入10个数：'))
d = eval(input('输入10个数：'))
e = eval(input('输入10个数：'))
f = eval(input('输入10个数：'))
g = eval(input('输入10个数：'))
h = eval(input('输入10个数：'))
i = eval(input('输入10个数：'))
j = eval(input('输入10个数：'))
m = ((a+b+c+d+e+f+g+h+i+j)/10)
d = pow((((a-m)**2+(b-m)**2+(c-m)**2+(d-m)**2+(e-m)**2+(f-m)**2+(g-m)**2+(h-m)**2+(i-m)**2+(j-m)**2)/9),1/2)
print('The mean is',m,'The standard deviation is',d)<jupyter_output>输入10个数：1
输入10个数：2
输入10个数：3
输入10个数：5.5
输入10个数：5.6
输入10个数：6
输入10个数：7
输入10个数：8
输入10个数：9
输入10个数：10
The mean is 5.71 The standard deviation is 2.9733819129065813
{'fraction_non_alphanumeric': 0.21878878133531893, 'fraction_numerical': 0.14755522462149417, 'mean_word_length': 3.4623477297895904, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 65, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '140982', 'license': 'Apache-2.0', 'n_tokens_mistral': 5057, 'n_tokens_neox': 4587, 'n_words': 856, 'path': '7.19.ipynb', 'repo_name': 'Kim0903/jincaifeng', 'size': '8058'}