<jupyter_start><jupyter_text>循环- 循环是一种控制语句块重复执行的结构- while  适用于广度遍历- for    开发中经常使用  while 循环- 当一个条件保持真的时候while循环重复执行语句- while 循环一定要有结束条件，否则很容易进入死循环- while 循环的语法是：    while loop-contunuation-conndition:        Statement<jupyter_code>while 1: # ture 无限循环
    print('you are ok')
i = 0
while i < 10:
    i += 1b
    print('yk')
string = 'abcd'
i = 0
while i < 4:
    print( string[i] )
    i += 1<jupyter_output>a
b
c
d<jupyter_text>示例：sum = 0i = 1while i <10:    sum = sum + i    i = i + 1  错误示例：sum = 0i = 1while i <10:    sum = sum + ii = i + 1- 一旦进入死循环可按 Ctrl + c 停止  EP：![](../Photo/143.png)![](../Photo/144.png)<jupyter_code>num = eval(input('Enter an integer:'))  # a) b) 无限循环 c) 9    a) b) 死循环  c) 语法错误
m = num
while num != 0:
    num = eval(input('Enter an integer:'))
    if num > m:
        m = num
print('m is ',m)
print('num',num)
i = 1
while i != 0:  # i 不能取到 0 只能无限接近 0 
    i -= 0.01
    print(i)<jupyter_output><empty_output><jupyter_text>验证码- 随机产生四个字母的验证码，如果正确，输出验证码正确。如果错误，产生新的验证码，用户重新输入。- 验证码只能输入三次，如果三次都错，返回“别爬了，我们小网站没什么好爬的”- 密码登录，如果三次错误，账号被锁定<jupyter_code>m = 0 #判断密码是否被锁定  3 为锁定  不是3 可解锁
import random
import string
passwd = ''.join(random.sample(string.ascii_letters + string.digits, 6))
print('密码：',passwd)
i = 0
if m != 3:
    while i < 3:
        password = input('Enter your password:')
        if passwd == password:
            print('your password is ture')
            for j in range(0,3):
                salt = ''
                salt = ''.join(random.sample(string.ascii_letters , 4))
                print('验证码：',salt)
                num = input('Enter four char:')
                if salt == num :
                    print('验证码正确')
                    break
                else:
                     print('验证码错误')
            if j == 2:
                print('别爬了，我们小网站没什么好爬的')
                break
            break
        else:
            print('your password is faluse')
            i += 1
        m = i
else:
    print('your acount is lock')
j = 0
while j < 3:
    yanzhengma = ''
    for i in range(4):
        random_1 = chr(random.randint(65,90))
        yanzhengma += random_1
    print(yanzhengma)
    input_ = input('>>')
    if input_ == yanzhengma
        print('OK')
        break
    else:
        print('ERROR')
        j += 1
    if j == 3:
        print('別爬了')
j = 0
password = '<PASSWORD>'
i = 0
if j != 3:
    while i < 3:
        pass_ = input('>>')
        if pass_ == password:
            print('passwword OK')
            break
        else:
            print('password error')
            i += 1
    j = i
else:
    print('your acount is lock')
                      
import matplotlib.pyplot as plt

x_list = []
y_list = []
for x in range(-50,50,1):
    y = x ** 2
    x_list.append(x)
    y_list.append(y)
print(x_list)
print(y_list)
plt.plot(x_list,y_list)
plt.show()


import matplotlib.pyplot as yk<jupyter_output><empty_output><jupyter_text>尝试死循环  实例研究：猜数字- 你将要编写一个能够随机生成一个0到10之间的且包括两者的数字程序，这个程序- 提示用户连续地输入数字直到正确，且提示用户输入的数字是过高还是过低<jupyter_code>import random
res = random.randint(0,10)
i = 11
print(res)
while i != res:
    num = eval(input('Enter a num:'))
    i = num<jupyter_output>3
Enter a num:1
Enter a num:2
Enter a num:3<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(1,1): 
    print(i)
for i in range(1,5,5):
    print(i)
import numpy as np
display(np.divmod.ran)
import matplotlib.pyplot as plt
image = plt.imread('C:\Desktop\yk.jpg.jpeg')
image.ndim<jupyter_output><empty_output><jupyter_text>在Python里面一切皆对象<jupyter_code>number = 0
summ = 0
i = 0
while i < 5:
    number = eval(input('Enter an integer:'))
    summ  += number
    i += 1
print('sum is :',summ)
print('count is ',i)<jupyter_output>Enter an integer:2
Enter an integer:3
Enter an integer:4
Enter an integer:5
Enter an integer:0
sum is : 14
count is  5<jupyter_text>EP:- ![](../Photo/145.png)<jupyter_code>summ = 0
i = 0
while i < 1001:
    summ = summ + i
    i += 1
print(summ)
summ = 0
for i in range(10000):
    if summ > 10000:
        break
    summ = summ + i
print(summ,i)<jupyter_output>10011 142<jupyter_text>嵌套循环- 一个循环可以嵌套另一个循环- 每次循环外层时，内层循环都会被刷新重新完成循环- 也就是说，大循环执行一次，小循环会全部执行一次- 注意：> - 多层循环非常耗时  - 最多使用3层循环  EP：- 使用多层循环完成9X9乘法表- 显示50以内所有的素数<jupyter_code>for i in range(1,10):
    for j in range(1,i+1):
        print(j,'*',i,'=',i*j,end = '  ')
    print('')
i = 1
while i < 50:
    summ = 0
    for j in range(1,i+1):
        if i % j == 0:
            summ += 1
    if summ == 2 :
        print(i,'是素数')
    i += 1
for i in range(2,50):
    for j in range(2,i):
        if i % j == 0:
            break
    else:
        print(i)
for i in range(2,50):
    if i == 2:
        print(i)
    for j in range(2,i):
        if i % j == 0:
            break
        if j == i - 1:
            print(i)
try:
    #尝试执行它
    
except:
    #try执行不了  执行
else:
    #except 执行不了  执行
finally:
    #最后都执行<jupyter_output><empty_output><jupyter_text>关键字 break 和 continue- break 跳出循环，终止循环- continue 跳出此次循环，继续执行  注意![](../Photo/56.png)![](../Photo/57.png)  Homework- 1 ![](../Photo/58.png)<jupyter_code>summ = 0
fsum = 0
zsum = 0
date = eval(input('Enter an integer,the input ends if it is 0:'))
while date != 0:
    if date > 0:
        zsum += 1
    if date < 0:
        fsum += 1
    summ += date
    date = eval(input('Enter an integer,the input ends if it is 0:'))
print('The avarege is ',summ/(zsum + fsum))<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
The avarege is  1.25<jupyter_text>- 2![](../Photo/59.png)<jupyter_code>summ = 10000
for i in range(0,10):
    summ += summ * (5 / 100)
    total = summ
print('ten year after :',summ)
num = 0
for j in range(0,4):
    num += total
    total += total * ( 5 /100 )
print('ten year after,four years',num)<jupyter_output>ten year after : 16288.946267774414
ten year after,four years 70207.39453239119<jupyter_text>- 3![](../Photo/58.png) - 4![](../Photo/60.png)<jupyter_code>j = 0
for i in range(100,1000):
    if i % 5 == 0 and i % 6 == 0:
        print(i,end = ' ')
        j += 1
        if j % 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>import math
i = 1
while i ** 2 < 12000:
    i += 1
print('min(n*n > 12000)',i)
j = 100
while math.pow(j,3) > 12000:
    j -= 1
print('max(n^3 < 12000)',j)<jupyter_output>min(n*n > 12000) 110
max(n^3 < 12000) 22<jupyter_text>- 6![](../Photo/62.png)<jupyter_code>import math
amount = eval(input('Loan Amount:'))
num = eval(input('Number of Years:'))
print('Interest Rate     Monthly Payment     Total Payment')
for i in range(5000,8001,125):
        f = amount * math.pow(( 1 + i / 100000 ),num)
        m = f / ( num * 12 )
        print( round(i / 1000,3) ,'%           ',round(m,2),'           ',round(f,2))<jupyter_output>Loan Amount:10000
Number of Years:5
Interest Rate     Monthly Payment     Total Payment
5.0 %            212.71             12762.82
5.125 %            213.98             12838.97
5.25 %            215.26             12915.48
5.375 %            216.54             12992.36
5.5 %            217.83             13069.6
5.625 %            219.12             13147.21
5.75 %            220.42             13225.19
5.875 %            221.73             13303.54
6.0 %            223.04             13382.26
6.125 %            224.36             13461.35
6.25 %            225.68             13540.81
6.375 %            227.01             13620.65
6.5 %            228.35             13700.87
6.625 %            229.69             13781.46
6.75 %            231.04             13862.43
6.875 %            232.4             13943.78
7.0 %            233.76             14025.52
7.125 %            235.13             14107.63
7.25 %            236.5             14190.13
7.375 %            237.88            [...]<jupyter_text>- 7![](../Photo/63.png)<jupyter_code>summ = 0
for i in range(1,50001):
    summ += 1 / i
print(summ)
total = 0
j = 50000
while j > 0:
    total += 1 / j
    j -= 1
print(total)<jupyter_output>11.397003949278504
11.397003949278519<jupyter_text>- 8![](../Photo/64.png)<jupyter_code>summ = 0
j = 1
for i in range(2,100):
    if i % 2 != 0:
        summ += j / i      
        j = i
print(summ)<jupyter_output>45.124450303050196<jupyter_text>- 9![](../Photo/65.png)<jupyter_code>import math
summ = 0
j = 1
for i in range(1,100001):
    if i % 2 != 0:
        j += 1
        summ += (1 / i) * math.pow(-1,j)
    if i % 10000 == 0:
        pi = 4 * summ
        print('pi = ',pi)<jupyter_output>pi =  3.141392653591791
pi =  3.1414926535900345
pi =  3.1415259869231935
pi =  3.1415426535898248
pi =  3.141552653589803
pi =  3.141559320256462
pi =  3.141564082161233
pi =  3.1415676535897985
pi =  3.141570431367571
pi =  3.1415726535897814<jupyter_text>- 10 ![](../Photo/66.png)<jupyter_code>for i in range(1,10001):
    summ = 0 
    for j in range(1,i):
        if i % j == 0:
            summ += j
    if summ == i:
        print(i,'是完全数')<jupyter_output>6 是完全数
28 是完全数
496 是完全数
8128 是完全数<jupyter_text>- 11![](../Photo/67.png)<jupyter_code>summ = 0
for i in range(1,8):
    for j in range(i,8):
        if i != j:
            print(i,j)
            summ += 1     
print(summ)<jupyter_output>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>import math
List = []
n = 0
summ = 0
for i in range(10):
    if i == 0:
        num = eval(input('Enter ten number:'))
        List.append(num)
        summ += num
        n += 1
    else:
        num = eval(input())
        summ += num
        n += 1
        List.append(num)
ave = summ / n
print('The mean is',ave)
#print(List)
m = 0
for j in range(10):
    m += math.pow((List[i] - ave),2)
deviation = math.sqrt( m / (n - 1) )
print('The standard deviation is',round(deviation,5))
1 **2 + 2**2 + 3**2 + 5.5 *2 + 5.6 **2 + 6 **2 + 7**2 + 8**2 + 9 **2 + 10 **2
1  + 2 + 3 + 5.5  + 5.6 + 6 + 7 + 8+ 9  + 10<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14431402732345583, 'fraction_numerical': 0.1094862420627285, 'mean_word_length': 2.115073419238837, 'pattern_counts': {'":': 0, '<': 78, '<?xml version=': 0, '>': 76, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '659820', 'license': 'Apache-2.0', 'n_tokens_mistral': 5226, 'n_tokens_neox': 4612, 'n_words': 1086, 'path': '7.19.ipynb', 'repo_name': 's1u9m9m0e1r/fzh', 'size': '10388'}